0

I am using Rebol2 and would like to persist a HASH! block.

Currently I am converting it to-string then using save.

Is there a better way? For example:

r: make hash! ["one" "two"]

I want to save this to a file, then load it back to r.

sqlab
  • 6,412
  • 1
  • 14
  • 29
Atomica
  • 23
  • 6

2 Answers2

1

you are very near your goal. Just use save/all and load

>> r: make hash! ["one" "two"]
== make hash! ["one" "two"]
>> save/all  %htest r
>> r: load %htest
== make hash! ["one" "two"]

If you want the same result in Red you just need one command more

>> r: do load %htest
== make hash! ["one" "two"]
sqlab
  • 6,412
  • 1
  • 14
  • 29
0

You do that just like with any other value - save, then load.

There are zero benefits in using hash! for persistent storage, by the way. What load gives you back is a plain block! ([make hash! [...]]). Populating hashtable from this loaded data takes more time compared to just loading block!, but gives you faster lookups thereafter.

In other words, you can just:

>> save %database [one two]
>> make hash! load %database
== make hash! [one two]

As described in the tutorial that you linked.

9214
  • 2,105
  • 11
  • 15
  • I'm working with hash! from the start. I'm trying to avoid having to convert to a block or string, then 'saving' it, only to load the block back and hash! it in the process. I don't mind doing that IF it's the better way (faster, smaller db storage) which is the question. save/all seems to avoid this hash! / dehash step. – Atomica Dec 02 '19 at 19:05
  • @Atomica `save/all` just uses construction syntax to store values and preserves their datatype (+ extra info in specific cases), that's all. `load` still needs to populate hashtable at runtime when you feed it `#[hash! [...]]`, or you need to `do` it manually in case of `[make hash! [...]]`. There are no speed / memory benefits from doing what you do - it's textual data either way (well, storing plain `block!` takes less characters, so it saves you a few bytes). – 9214 Dec 02 '19 at 19:24
  • Well, in that case, save/all is better as it saves the steps. – Atomica Dec 02 '19 at 19:51