Reputation: 2754
let's say I have a config.txt which contains:
"param11" "param12"
"param21" "param22"
I'll load it in memory with
config: load %config.txt
I can save it back with
save %config.txt config
So far so good. Now the problem occurs for me when I want to add
"param31" "param32"
I have tried
append config reduce [newline "param31" "param32"]
save %config.txt config
But that doesn't give the expected result
"param11" "param12"
"param21" "param22"
"param31" "param32"
but this instead
"param11" "param12"
"param21" "param22" #"^/" "param31" "param32"
So how to ?
Upvotes: 1
Views: 631
Reputation: 71
NOT RIGHT: write/append %config.txt reform [newline "param31" "param32"]
Answer #1 isn't quite right. Here's the result when looking at the text file with an editor app:
"param11" "param12"
"param21" "param22"
param31 param32
Here's the result upon loading the file after the write/append:
>> write/append %config.txt reform [newline "param31" "param32"]
>> a: load %config.txt
== ["param11" "param12"
"param21" "param22"
param31 param32
]
Thus param31 and param32 are not saved as strings but as words.
>> first a
== "param11"
>> type? first a
== string!
>> fifth a
== param31
>> type? fifth a
== word!
RIGHT: write/append %config.txt reform [newline mold "param31" mold "param32"]
>> write/append %config.txt reform [newline mold "param31" mold "param32"]
>> b: load %config.txt
== ["param11" "param12"
"param21" "param22"
"param31" "param32"
]
>> type? b/5
== string!
Upvotes: 1
Reputation: 33637
We know that:
[a
b]
and
[a b]
...are treated the same as Rebol "source code", they describe identical data structures. We also know that Rebol is reflective and can read and write its code/data format with LOAD and SAVE.
Yet this raises the question: If you LOAD a source file with a newline in it... and then SAVE it... will there be a line break in the output? Or will it be forced into a canonical format?
(For a bit of crazy background on the LOAD and SAVE wackiness, read my debate with BrianH...where you see me making a solid case and him brushing me off as per usual. It's frustrating to be at odds with the "gurus", however clever they may be. :P)
In any case, you must realize that Rebol is trying to be clever. There's a binary "new-line" bit which is hiding behind the scenes, do help new-line
and you'll see:
USAGE:
NEW-LINE block value /all /skip size
DESCRIPTION:
Sets or clears the new-line marker within a block. (Modifies)
NEW-LINE is a native value.
ARGUMENTS:
block -- Position in block to change marker (block!)
value -- Set TRUE for newline
REFINEMENTS:
/all -- Set/clear marker to end of block
/skip -- Set/clear marker periodically to the end of the block
size (integer!)
So if you want to play the Rebol source code game, and use LOAD and SAVE, you must invoke this API to inject a "new-line" bit onto param22
. But realize that if your data must fit a non-Rebol-source wire format (or be read by human readers with certain expectations) then you're using the wrong functions. You need to do rank-and-file serialization with read
and write
.
Upvotes: 3
Reputation: 2307
As Fork's said load/save is for Rebol readable data. Read/Write is for general use.
write/append %config.txt reform [newline "param31" "param32"]
will work in your case. %config.txt is still loadable.
Upvotes: 2