Load Hash Map - Object Key Pair
Please consider this example when I am saving map!
to a file and want to load it back and access its keys and values. Its keys are hashes (INTEGER !, computed checksum
), and its values ββare blocks:
Rebol []
bl1: make object! [
name: "first"
age: 42
]
bl2: make bl1 []
bl2/name: "second"
bl2/age: 30
hash1: checksum to-binary to-string bl1
hash2: checksum to-binary to-string bl2
m1: make map! []
repend m1 [hash1 bl1]
repend m1 [hash2 bl2]
save %./map_example.ls m1
m2: do load %./map_example.ls
probe join "m2: " type? m2
It returns "m2: map!"
The % map_example.ls file consists of:
make map! [
2749161 make object! [
name: "first"
age: 42
]
4111405 make object! [
name: "second"
age: 30
]
]
How can I read map!
back to access my keys and values, which are objects?
Is there a foreach
way to go?
source to share
SAVE
uses an imperfect but more readable format. Use SAVE/ALL
to keep all values ββexactly the way they should be ( SAVE/ALL
uses the call serialization format in the form #[datatype! value]
).
Also, just use LOAD
and not DO LOAD
to return data. DO
not required in this case - LOAD
converts the string to Rebol values, and once the data, not the script, there is no need DO
(it can, but it just returns itself).
source to share