[REBOL] Setting variable in complex paths
From: mike::yaunish::home::com at: 27-Apr-2001 12:53
I have been working with complex paths in REBOL because I just love how
easy they are
to work with - however I have had some problems setting variables within a
complex
path. I think I have a fairly good solution, but know if I expose it to the
light of other REBOLers
it can be even better. I have called this function "complex-set".
complex-set: func ["Sets a complex path (from a block of literal words) to
a specified value"
complex-path [block!] "Block of literal and normal
words that describe the complex path"
value [any-type!] "Value to be set"
][
either lit-word? last complex-path [
prefix-path: copy/part complex-path ((length? complex-path) - 1 )
the-path: to-path reduce prefix-path
the-field: to-lit-word last complex-path
either ( found? find the-path the-field ) [
do bind reduce [to-set-path reduce complex-path value] 'do
][ ; field doesn't already exist so create it
append/only the-path the-field
append/only the-path value
]
][
do bind reduce [to-set-path reduce complex-path value] 'do
]
]
address-book: [ [name [first "Joe" last "Blow" ] address [city "Goosebay"
province "Labrador"]]
[name [first "Tim" last "Taylor" middle "Lee"] address [city
Calgary
province "Alberta"]]
]
>>index: 2
>>new-name: "Tiny"
>>complex-set [ 'address-book index 'name 'middle ] new-name
complex-set
also accomodates the creation of fields within a path.
For example;
>>complex-set [ 'address-book 1 'name 'middle ] "Lincoln"
>>complex-set [ 'address-book 2 'address 'zip ] 90210
>> print mold address-book
[[name [first "Joe" last "Blow" middle "Lincoln"] address [city "Goosebay"
province "Labrador"]]
[name [first "Tiny" last "Taylor" middle "Tiny"] address [city
Calgary
province "Alberta" zip 90210]]
]
There are some minor limitations - but it works great for REBOL named fields.
Any comments would be appreciated.
Mike Yaunish
[mike--yaunish--home--com]