[REBOL] Antwort: Compiler for Rebol ? Re:(15)
From: joel:neely:fedex at: 28-Sep-2000 7:07
Well, Sharriff, it depends on what you want to select...
Using cut-n-paste from your original note:
>> 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...
>> select foo 'admin
== [
name "Sharriff Aina"
password "gong"
email [sharriff--aina--med-iq--de]
dept "Administration"
view "yes"
]
>> select foo "admin"
== none
>> select foo admin
** Script Error: admin has no value.
** Where: select foo admin
Note that all of your "key" values are of type word! and NOT of
type string! which means you must use a literal word or an
expression that evaluates to a value of type word! in order to
get a match.
OTOH, if you want to search by name, you can either write
a function to search the nested blocks, as in
>> searchname: func [who [string!] /local k v] [
[ foreach [k v] foo [
[ if v/name = who [return v]
[ ]
[ ]
>> searchname "Sharriff Aina"
== [
name "Sharriff Aina"
password "gong"
email [sharriff--aina--med-iq--de]
dept "Administration"
view "yes"
]
OTOH, (wait a minute... I don't have three hands! Can somebody
give me a hand? ;-) if you're going to do lots of searching in
that data structure, it's worthwhile to pre-build another data
structure that can serve as an index or cross-reference to the
original one:
>> buildxref: func [/local xref k v] [
[ xref: copy []
[ foreach [k v] foo [
[ append xref v/name
[ append/only xref v
[ ]
[ xref
[ ]
>> fum: buildxref
== ["Sharriff Aina" [
name "Sharriff Aina"
password "gong"
email [sharriff--aina--med-iq--de]
dept "A...
>> select fum "Sharriff Aina"
== [
name "Sharriff Aina"
password "gong"
email [sharriff--aina--med-iq--de]
dept "Administration"
view "yes"
]
Modifying either of the above to return both the key and value from
the ORIGINAL block (e.g., getting back BOTH the key 'admin and the
block of associated data), is a minor tweak that is left to the
reader.
(That's academicspeak for
"I don't have time to type it right now."
but feel free to ask if you want any followup! ;-)
-jn-
[Sharriff--Aina--med-iq--de] wrote: