tags:

views:

41

answers:

2

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 ?

+2  A: 

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.

Hostile Fork
Never heard of new-line will be usefull anyhow andWill read wiki this week-end seems funny :)
Rebol Tutorial
+1  A: 

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.

endo64
Thanks have finally done like both of you said.
Rebol Tutorial