[REBOL] Re: hash questions
From: dlhawley:attbi at: 8-Jan-2002 22:02
Thanks Joel and others. I modified Joel's code (below).
changes:
data: make hash![] -- also needed in clear
append only appends one value - so I append the key, then the value
Also I added a simple to/from-file persistance. Does anyone know
whyREBOL
has the asymmetry in in read/write? - fortunatly reduce fixes things
up nicely.
Here's what I'm looking at now:
assoc: make object!
[ data: make hash! []
default: none ; return for no key match
file: none ; to/from-file
clear: does [ data: make hash! [] ]
store: func [ k v /loc pos]
[ pos: find/skip data k 2
either found? pos
[ change next pos v]
[ append data k
append/only data v
]
]
fetch: func [ k /local r]
[ either found? r: select/skip data k 2 [first r] [default]]
to-file: does [ write file mold data ]
from-file: func [][ data: reduce do read file ]
]
assoc-test: func []
[
hd: make assoc [ file: %test.rdb ]
hd/store "ob1" [ 1 2 3]
hd/store "ob2" [ "text data" ]
hd/store "ob3" make object! [ f1: "text data" f2: 24 ]
print mold hd/fetch "ob1"
hd/to-file
hd/from-file
print mold hd/fetch "ob1"
print mold hd/fetch "ob2"
print mold hd/fetch "ob3"
]
>> assoc-test
[1 2 3]
[1 2 3]
["text data"]
make object! [
f1: "text data"
f2: 24
]