Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Find on a nested structure

 [1/8] from: sanghabum:aol at: 11-Apr-2001 12:30


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.

 [2/8] from: gjones05:mail:orion at: 11-Apr-2001 12:23


From: <[Sanghabum--aol--com]>
<snip> > Can you guys help me on this one?
<<quoted lines omitted: 18>>
> 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

 [3/8] from: ryanc:iesco-dms at: 11-Apr-2001 10:24


This should work, although it could be alot better. --Ryan ;;;;;;;;;;;;;;;;;;;;;;;; MyStructure: [ ["US" ["USA" "English" "Spanish"] ] ["DE" ["Germany" "German"] ] ["NZ" ["New Zealand" "English"] ] ] OtherStructure: [ words [ a b c ] numbers [1 2 3 4 5] blocks-of-strings [ [ "A" "B" "C" "D" "E" ] [ "F" "G" "H" ] ["I"] ] deep-path [ etc [ profile [ %/etc/profile ] passwd [%/etc/profile] ] usr [ bin [ X11 [ %X ] ] ] ] ] deep-find: func [ "Searches for a particular element in a block, recursing sub-blocks." haystack [block!] "Block to search." needle [any-type!] "Element to search for." ][ foreach hay haystack [ if hay = needle [ return true] ; Ouch! if all [ block? hay deep-find hay needle ] [ return true] ; More haystack : ( ] return false ] confirm join "Find US (TRUE): " deep-find MyStructure "US" confirm join "Find AU (FALSE): " deep-find MyStructure "AU" confirm join "Find %X (TRUE): " deep-find OtherStructure %X confirm join "Find %command.com (FALSE: " deep-find OtherStructure %command.com ;;;;;;;;;;;;;;;;;;;;;;;; [Sanghabum--aol--com] wrote:
> Can you guys help me on this one? > I've got a structure like this:
<<quoted lines omitted: 21>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400 I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world. -Einstein

 [4/8] from: ryanc::iesco-dms::com at: 11-Apr-2001 10:58


Ah, very good points Scott, and clean solution. Using deep-find for that is like using a chainsaw to prune a rose bush. --Ryan GS Jones wrote:
> From: <[Sanghabum--aol--com]> > <snip>
<<quoted lines omitted: 71>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400 I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world. -Einstein

 [5/8] from: petr:krenzelok:trz:cz at: 11-Apr-2001 21:30


Highly ineffective :-) I already suggested find/deep (or 'locate), which would return block of position or lit-path to be implemented in 'native C level code ... -pekr-

 [6/8] from: koolauscott:y:ahoo at: 11-Apr-2001 22:13


I've been doing the following with a similar problem: MyStructure: [ ["US" ["USA" "English" "Spanish"] ] ["DE" ["Germany" "German"] ] ["NZ" ["New Zealand" "English"] ] ] search: func [search-term [string!]][ foreach item MyStructure [ if find first item search-term [ return second item ] ] ] Example:
>> search "NZ"
== ["New Zealand" "English"]
>> search "DE"
== ["Germany" "German"] I hope this is what you were asking for. --- [Sanghabum--aol--com] wrote:

 [7/8] from: agem:crosswinds at: 12-Apr-2001 13:04


[rebol [title: "funny search"] MyStructure: [ is-is? ["US" ["USA" "English" "Spanish"]] is-is? ["DE" ["Germany" "German"]] is-it? ["NZ" ["New Zealand" "English"]] ] look-for: "DE" found: none is-it?: func [block] [ if look-for = first block [ found: block ] ] do MyStructure ? found ;-) ]

 [8/8] from: sanghabum:aol at: 15-Apr-2001 7:28


Thanks to everyone who replied to this. I've adopted Scott's approach. I guess my problem was twofold: expecting FIND to do more than it does; and being unable to fathom SORT. Thanks again, Colin.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted