[REBOL] Re: hash questions
From: rotenca:telvia:it at: 8-Jan-2002 17:08
Hi David, Joel and all
> You can do this in REBOL:
>
> favoriteLanguage:
> [ "Larry" "Perl"
> "Carl" "REBOL"
> "Guido" "Python"
> "Matz" "Ruby"
> "Richard" "LISP"
> ]
>
> and then
>
> >> print select favoriteLanguage"Guido"
You can do also:
name: "Guido"
print favoriteLanguage/:name
The path evaluator make an implict select on block and hash.
But the problem here is that if a language called "Guido" appears before the
user "Guido" then fails, because the path evaluator doesn't skip anything so
it can be used if datatype are different, for example: word! - string! or
string! - file! or file! - block! string! integer! and so on.
A fetch based on this:
fetch: func [k [string!]] [data/:k ]
but if k is not found fetch will fire an error (Invalid path value).
It is possible to do also a change, if the index is a word (it seems to me a
strange thing - what do you think?):
data: [guido "Rebol"]
change: func [k [word!] value] [k: to-set-word k data/:k :value]
change 'guido "What else?"
> However, if you want to maintain a dynamic set of associations
> it's a bit more work:
>
> assoc: make object!
> [ data: []
> default: 0
> clear: func [] [data: copy []]
> store: func [k [string!] v [string!] /local pos]
> [ pos: find/skip data k 2
> either found? pos
> [ change next pos v]
> [ append append data k v]
> ]
> fetch: func [k [string!] /local r]
> [ either found? r: select/skip data 2 k [r] [default]]
---
Ciao
Romano