[REBOL] Re: Creating a list, writing it in a file
From: carl:cybercraft at: 12-Apr-2002 0:03
On 11-Apr-02, COFFRE Richard FTO wrote:
> Hi,
> Reading lines in a file, I want to create a list which each item is
> a line, e.g. :
> foo
> azer
> redc
> ertg
> =>
> routines: ["foo" "azer" "redc" "ertg"]
> Currently I use "append" as follows :
> append routines ligne
> But I have something with no spaces between the items, therefore
> when I sort the serie it is an character sorting and I obtain
> something like "acdeeefgoorrrrtz" while I want ["azer" "ertg" "foo"
> "redc"]
Hi Richard,
I assume you mean it's starting out as a list of lines you want to
sort and not a block? ie...
>> lines: "aaa^/ccc^/bbb"
== "aaa^/ccc^/bbb"
>> print lines
aaa
ccc
bbb
If so, first convert it to a block using parse then sort the block...
>> blk: parse/all lines "^/"
== ["aaa" "ccc" "bbb"]
>> sorted: sort blk
== ["aaa" "bbb" "ccc"]
You can then save the block as a text-file using write/lines...
>> write/lines %test.txt sorted
and read it back in as either a string or a block...
>> read %test.txt
== "aaa^/bbb^/ccc^/"
>> read/lines %test.txt
== ["aaa" "bbb" "ccc"]
> At the end to copy it in a file, can I use the following instruction
> forall routines [write/append/lines output first routins]
write/append/lines adds lines to an existing file, so the above would
add all of routines to a file. (Typos excepted:) If you want to
overwrite a current file or start a new one though, just use
write/lines as I've done above, there being no need for a loop.
HTH.
--
Carl Read