[REBOL] Re: Find on a nested structure
From: gjones05:mail:orion at: 11-Apr-2001 12:23
From: <[Sanghabum--aol--com]>
<snip>
> Can you guys help me on this one?
>
> I've got a structure like this:
>
> MyStructure: [ [key1 data1] [key2 data2] [key3 data3] ... ]
>
> where each Key is a single string, but the Data is another block.
>
> Example:
>
> MyStructure: [
> ["US" ["USA" "English" "Spanish"] ]
> ["DE" ["Germany" "German"] ]
> ["NZ" ["New Zealand" "English"] ]
> ]
>
> I can't find any way to use Find to find me a given key:
>
> >> find MyStructure "US"
> == none
> >> find MyStructure ["US"]
> == none
>
> Is there something I can do with Find?
>
> I don't want to change the structure to
>
> MyStructure: [ key1 [data1] key2 [data2] key3 [data3] ... ]
>
> which is Find-able, but isn't SORT-able,--or at least not by me.
>
> Thanks,
> Colin.
Hi, Colin,
Been there and agonized over the same things. You are going to be much
happier if you do your second option. 'Find works more seemlessly and sort
accomplished using the /skip refinement, such as:
MyStructure: [
"US" ["USA" "English" "Spanish"]
"DE" ["Germany" "German"]
"NZ" ["New Zealand" "English"]
]
sort/skip mystructure 2
---
yields
[
"DE" ["Germany" "German"]
"NZ" ["New Zealand" "English"]
"US" ["USA" "English" "Spanish"]
]
If you are intent on the first form (of enclosing each grouping in its own
block), as far as I know you will need to iterate through the structure and
perform a 'find on each group, such as:
MyStructure: [
["US" ["USA" "English" "Spanish"] ]
["DE" ["Germany" "German"] ]
["NZ" ["New Zealand" "English"] ]
]
foreach s mystructure [if (find s "US") [print s]] ;yields US USA English
Spanish
foreach s mystructure [if (find s "DE") [print s]] ; yields DE Germany
German
Although I know what I've said is not what you are really looking for, I
hope the 'sort refinement eases your "pain." (I guess I'll have to "feel
your pain" since the current US President no longer does!!) I've set up
very complex and long data structures using this method, and it works great
(, even without making it a hash).
--Scott Jones