2

I'm stuck on this after many attempts, I have an array of items and I'm trying to output this to a file but the problem is that it writes all at once and ignores newline. I'm beginning to wonder if rebol even has such a simple ability. file1.txt contains multiple lines

myArray: []
foreach line read/lines %file1.txt [
    append myArray line
]
write %file2.txt myArray

this does not work, everything is written on to one line

fp: open/new %file2
foreach line myArray [insert fp line]
close fp

Neither does that work "cannot use insert on port!"

I am not trying to copy a file, The above is just a demonstration of what i'm trying to do.

draegtun
  • 22,441
  • 5
  • 48
  • 71
sgroves855
  • 153
  • 10
  • In your second example you need to use `write` instead of `insert`. So you could do `foreach line myArray [write fp join line newline]` or just `write/lines fp myArray` – draegtun Jun 04 '15 at 12:27

2 Answers2

3

Rebol keeps newlines as they are. But after reading with read/lines you just get a block of items without the newlines. If you want a block of items written as lines separated by newlines, you should write them again with the refinement write/lines and Rebol adds the newlines again.

myArray: []
foreach line read/lines %file1.txt [
    append myArray line
]
write/lines %file2.txt myArray
sqlab
  • 6,412
  • 1
  • 14
  • 29
  • Additionally don't forget that you can SAVE your Rebol data and LOAD it back when necessary. SAVE also handles newlines if you say so with NEW-LINE function. – endo64 Jun 08 '15 at 16:54
1

When you use read/lines Rebol discards the line ending data and gives you an block of strings. If you want to write the block to a file you can add in the newline to each line.

myArray: []
foreach line read/lines %file1.txt [
    append myArray join line newline
}
write %file2.txt myArray
johnk
  • 1,102
  • 7
  • 12