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

[REBOL] Antwort: Antwort: Compiler for Rebol ? Re:(16)

From: joel:neely:fedex at: 28-Sep-2000 14:34

[Sharriff--Aina--med-iq--de] wrote:
> Thanks Jeff!! one question: > > the "local" value "k", what is it piroüose exactly , its not beineg > breferenced at all >
Hey, I did say I'd left something as an exercise! ;-)
> >> buildxref: func [/local xref k v] [ > [ xref: copy [] > [ foreach [k v] foo [ > [ append xref v/name > [ append/only xref v > [ ] > [ xref > [ ] >
If the second argument of foreach is a list of words, then they take consecutive values from the block that is the second argument.
>> foo: [1 "a" 2 "b" 3 "c" 4 "d"]
== [1 "a" 2 "b" 3 "c" 4 "d"]
>> foreach [n s] foo [print s]
a b c d
>> foreach [n s] foo [print n]
1 2 3 4
>> foreach [n s] foo [print [n "->" s]]
1 -> a 2 -> b 3 -> c 4 -> d
>>
So, all I was saying in buildxref was "Walk through foo , treating it as a collection of pairs. Call the left-hand element k and the right-hand element v while looking at each pair. Now, here's what to do with each pair..." It just happens that the simplified version if cross-referencing I showed in that example didn't use the left-hand element. There are other ways to skin this cat. For example, using forskip as illustrated by:
>> skipfoo: next foo
== ["a" 2 "b" 3 "c" 4 "d"]
>> forskip skipfoo 2 [print first skipfoo]
a b c d == false
>> skipfoo: foo
== [1 "a" 2 "b" 3 "c" 4 "d"]
>> forskip skipfoo 2 [print first skipfoo]
1 2 3 4 == false
>> skipfoo: foo
== [1 "a" 2 "b" 3 "c" 4 "d"]
>> forskip skipfoo 2 [print [first skipfoo "->" second skipfoo]]
1 -> a 2 -> b 3 -> c 4 -> d == false
>>
Both of these approaches can be used with groupings of more then two elements at a time. For instance, if I had a block containing ID#, name, and phone number for a bunch of employees (I'll not bother to make up sample data for this one), one could list all of the names by saying either (*WARNING* *UNTESTED CODE FRAGMENT*) foreach [emp-id emp-name emp-phone] emp-data-block [ print emp-name ] or emp-temp-block: next emp-data-block forskip emp-temp-block 3 [print first emp-temp-block] or (a TOTALLY UNSATISFACTORY solution, in my opinion) emp-data-block: next emp-data-block forskip emp-data-block 3 [print first emp-data-block] emp-data-block: head emp-data-block The first of these three (at least to my eye) seems to do the best job of making clear that I'm dealing with emp-data-block as a collection of triples, and making clear exactly what I'm doing with (what parts of) each triple. -jn- PS: Since I've already blabbered on for this long, I might as well include one solution to the open issue I raised in the earlier post in this thread -- creating an index/crossreference to your original block that lets me start with a name and get at both the key and value block from the original data structure: == false
>> foo: [
[ admin [ [ name "Sharriff Aina" [ password "gong" [ email [sharriff--aina--med-iq--de] [ dept "Administration" [ view "yes" [ ] [ [ sharriff [ [ name "Sharriff Aina" [ password "jungle" [ email [sharriff--aina--med-iq--de] [ dept "Administration" [ view "yes" [ ] [ ] == [ admin [ name "Sharriff Aina" password "gong" email [sharriff--aina--med-iq--de] dept "Admini...
>> buildxref: func [orig [block!] /local xref k v] [
[ xref: copy [] [ foreach [k v] orig [ [ append xref v/name [ append/only xref reduce [k v] [ ] [ xref [ ]
>> fum: buildxref foo
== ["Sharriff Aina" [ admin [ name "Sharriff Aina" password "gong" email sharriff.ain...
>> first select fum "Sharriff Aina"
== admin This version uses both the key and value elements of each pair. Of course, I can't let this drop without assigning more homework (old teaching habits die hard ;-), so let's notice that the name "Sharriff Aina" occurs more than once in your original data. Wouldn't it be nice to have a version of buildxref that made a crossreference structure that paired each name with a block of key/value pairs, for all entries in the original data where the name appeared? -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] print to-string debase decompress #{ 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}