World: r3wp
[I'm new] Ask any question, and a helpful person will try to answer.
older newer | first last |
mhinson 21-May-2009 [2755] | Hi, I am puzzling over this and would really appreciate some pointers please. How do I get from this: d1: "random1" d2: "data2" ;;to this? b1: [random1 ["data2"]] so I can reference the data2 by its association with random1 e.g. b1/random1/1 |
Henrik 21-May-2009 [2756] | well: b1: reduce [to-word d1 reduce [d2]] |
mhinson 21-May-2009 [2757x3] | ah, nice. Thanks. I was looking here http://www.rebol.com/docs/core23/rebolcore-16.html#section-3.10 which for once looks like it was the right area. Thank you. |
I was hoping to get something like this working with the data structure Henric helped me with above. foreach bb b1 [ print b1/:bb/1] but the foreach itterates all the vlaues, not just the words. I have been thinking about this for a week or so, but dont really know how to get any further with it. I suspect that if I had the right structure I could use it to store my data, then add additional details when I find some more, and finaly export the whole lot, grouped together by the key value (that I will only know when the script is running). Im aproaching this in the right sort of way? Or is there a recognised way to create these sort of structures please? | |
Sorry about my typing Henrik. I know how to spell your name really. | |
BrianH 21-May-2009 [2760] | foreach [key val] b1 [print val] |
mhinson 21-May-2009 [2761] | That is just what I wanted, thanks Brian. Is this a recognised way to deal with data that is presented in a different order to the output requirement? |
BrianH 21-May-2009 [2762x2] | No, it's just a way to treat a block as fixed records, this time of two values each. |
If you want variable records you either put the data in an inner block (as you have), or use a distinct datatype for the keys ans search for values of that datatype to find the next key. | |
mhinson 21-May-2009 [2764] | I think I need to learn more before I can use this to the effect I need. I was expecting your construct to all me to do this: foreach [key val] b1 [print b1/:val/1] |
BrianH 21-May-2009 [2765] | I don't get what you want to do. Perhaps some sample data and the desired output? |
mhinson 21-May-2009 [2766] | probe b1 [random3 ["data4"] random1 ["data2"]] probe b1/random1/1 data2 but as I dont know what random1 is I want to enumerate the values from the data structure. |
BrianH 21-May-2009 [2767] | Are you trying to get a specific data* or to enumerate all of random*? |
mhinson 21-May-2009 [2768] | Ah I think I am being a numpty. your structure already returns the data, but I need to be able to print the keys with the associated data. |
BrianH 21-May-2009 [2769x2] | foreach [r d] b1 [print [r d]] |
You can do some formatting in the print statement, but it is that easy. | |
mhinson 21-May-2009 [2771] | I will have different numbers of data elements in different keys, so I also need to keep track of which data is stored where, perhaps I need an array as the data element. |
BrianH 21-May-2009 [2772x3] | Your data already has an array as the data element, except we call them blocks. |
b1: [random3 ["data1" "data2"] random1 ["data3"]] | |
foreach [r d] b1 [print [r mold d]] | |
mhinson 21-May-2009 [2775] | I need to know what type of data I have put in data3. It might be the same type as in data1 or data2, or something different again. any one of maybe 10 types. The actual data will be IP addresses & interface details & remote connection information. |
BrianH 21-May-2009 [2776x3] | You can specify ip addresses directly, as data of the tuple! type. Or you could have the data be doubles of strings and type flags. |
Or if you would have at most one of each type, you could name the potential fields and have the data be name value pairs. | |
*a block of* name value pairs. | |
mhinson 21-May-2009 [2779] | most of my different types will be strings I am expecting. Sometimes there will be up to about 4 of the same type (addresses & secondary addresses mostly) |
BrianH 21-May-2009 [2780x2] | [key [flags ["flag1" "flag2"] ips ["127.0.0.1" "192.168.1.1"]] |
Sorry, missing a last ] | |
mhinson 21-May-2009 [2782] | The pairs idea sounds productive, so it is a highly structured array with named groups? |
BrianH 21-May-2009 [2783x2] | Or you could go positional for the different types of data, instead of including the flags and ips words all of the time. dat: [key [["flag1" "flag2"] ["127.0.0.1" "192.168.1.1"]]] foreach [key val] dat [set [flags ips] val ...] |
Structured data makes things easier. | |
mhinson 21-May-2009 [2785] | Thank you very much for you time & help. I will need a while to digest this & make it work with my existing code. I think this type of structure is going to be the core of most of the data extraction I need to do, so I must get to understand it very well. I have a section that uses an array, but some of the more interesting data manipulation needs to cope with more varied keys. Thanks. |
BrianH 21-May-2009 [2786] | This a pretty standard way for REBOL to do lightweight data structures. Blocks are lighter-weight than objects. Enjoy :) |
RobertS 22-May-2009 [2787] | . |
mhinson 22-May-2009 [2788x2] | Hi, is there an easy way to make this sort of structure work please? data: [bike [wheels [2] owner [john]] pig [legs [4] owner [roger]]] foreach [thing content] data [print [data/:thing/owner "~" data/:thing/wheels "~" data/:thing/legs]] I want the invalid references (e.g. data/bike/legs) to return nothing so the list is printed for both things |
I know I could put "" values in when I create the structure, but I wondered if the was a neater way to do this. | |
Henrik 22-May-2009 [2790x2] | if you wrap a path in ATTEMPT, it will return none. |
when it doesn't exist | |
mhinson 22-May-2009 [2792] | Thanks Henrik. That sounds like what I need. |
Henrik 22-May-2009 [2793] | I like to do things like: any [attempt [this/leads/nowhere] "Unknown"] |
mhinson 22-May-2009 [2794x2] | The task I am trying to accomadate is to output my data as a table. so I think I can adapt your example to return "" Thanks. |
I cant work out how to turn that into a function, because if I do att: func [paf][any [attempt [paf] ""]] att this/leads/nowhere the invalid path returns an error before it can be passed to the function. I bet there is a good answer. I have been looking at using :paf, but that makes it return the invalid path | |
Steeve 22-May-2009 [2796] | lit-path!, it's done for that. |
mhinson 22-May-2009 [2797x3] | so I try to find a way to pass it as that sort of data type? |
Not getting anywhere with this I am afraid. where do I put the lit-path! please? | |
I have been trying this sort of thing att: func [paf[lit-path!]][any [attempt [paf] ""]] but I suspect that is not what Steeve means. | |
Steeve 22-May-2009 [2800] | You suspect well, but you suspect too much. It would be better if you knew all the data types in Rebol and their usages. Currently you're only trying to "guess" how to programm with Rebol, not to learn it. I don't think it's always a good method to "give" the answer directly, especially when they are obvious. It' gives some bad habits to the newcommers. Sorry. |
Graham 22-May-2009 [2801x2] | Steeve is an advocate of the Socratic method |
BTW, I think any [ attempt [paf] "" ] should be any [ attempt [.. ] copy "" ] ... ie. you need a 'copy in there. | |
mhinson 22-May-2009 [2803] | ok, I will continue to read the documentation. I think you are right Steeve, but it is hard to learn with no current programing skills. Perhaps I am just trying to find out how to solve my problems. Some of the data types seem quite straight forward, but some seem to need to be used before I can appreciate what they are exactly.. I try not to ask questions that are too trivial, and never before I have tried to research an answer for myself. You maybe just misunderstand how stupid I am. ;-) |
Graham 22-May-2009 [2804] | There are several good books on REBOL, even one in english |
older newer | first last |