Creating a list, writing it in a file
[1/9] from: richard:coffre:francetelecom at: 11-Apr-2002 10:48
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"]
At the end to copy it in a file, can I use the following instruction
forall routines [write/append/lines output first routins]
TIA
Richard Coffre
France Telecom Orbiscom
T=E9l. : 01 47 61 46 28
[2/9] 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
<<quoted lines omitted: 11>>
> 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
[3/9] from: richard:coffre:francetelecom at: 11-Apr-2002 15:11
Hi Carl,
Some details, my purpose is to create a list while reading lines in a file,
then to sort the list and finally to copy it in a new file so first I have
to extract the lines to create the list, I don't have it.
What's the weather like in New Zealand ?
Richard
-----Message d'origine-----
De : Carl Read [mailto:[carl--cybercraft--co--nz]]
Envoy=E9 : jeudi 11 avril 2002 14:04
=C0 : [rebol-list--rebol--com]
Objet : [REBOL] Re: Creating a list, writing it in a file
On 11-Apr-02, COFFRE Richard FTO wrote:
> Hi,
> Reading lines in a file, I want to create a list which each item is
<<quoted lines omitted: 11>>
> 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
[4/9] from: joel:neely:fedex at: 11-Apr-2002 14:48
Hi, Richard,
COFFRE Richard FTO wrote:
> Hi,
>
> Reading lines in a file, I want to create a list which each item
> is a line,
>
Perhaps I don't understand, but here's a sample file:
8<----------lines.txt----------
this
is
a
simple
example
of
line
by
line
text
input
8<-----------------------------
here's a REBOL one-liner that uses it:
8<------------------------------
write/lines %sorted.txt sort read/lines %lines.txt
8<------------------------------
and here's the resulting data file:
8<---------sorted.txt-----------
a
by
example
input
is
line
line
of
simple
text
this
8<------------------------------
Hope this helps!
-jn-
[5/9] from: carl:cybercraft at: 12-Apr-2002 9:40
On 12-Apr-02, COFFRE Richard FTO wrote:
> Hi Carl,
> Some details, my purpose is to create a list while reading lines in
> a file, then to sort the list and finally to copy it in a new file
> so first I have to extract the lines to create the list, I don't
> have it.
Well, would this work...
a-list: read/lines %original-file.txt
new-list: sort a-list
write/lines %new-file.txt new-list
If not, then there's something I'm not understanding...
Hmmm. Unless you want to read a line in at a time for some reason?
In that case, open a port for reading your file. ie...
a-port: open/lines %original-file.txt
You can then access the file via the port as if it was a normal
series. ie...
a-list: copy [] ; Block to put lines in.
forall a-port [
append a-list first a-port
]
Then close the port...
close a-port
The lines will now be in the a-list block and can be sorted and saved
as with my first example.
> What's the weather like in New Zealand ?
A bit grey this morning...
--
Carl Read
[6/9] from: richard:coffre:francetelecom at: 12-Apr-2002 8:40
Hi Carl and Joel
I read your last answers but I wasn't precise enough in my query. My job is
a little more complex.
Purpose:
1. Read the line and extract (if necessary) a function name.
2. Check if this function is already stored in my list, if not do it.
3. Once the file is read, sort the list
4. Write the list in a file.
The step 2 explains why I can't just read the file and put it right in a
list.
However, I think I could solve my problem with the Carl's answer.
[7/9] from: carl:cybercraft at: 12-Apr-2002 20:17
On 12-Apr-02, COFFRE Richard FTO wrote:
> Hi Carl and Joel
> I read your last answers but I wasn't precise enough in my query. My
<<quoted lines omitted: 6>>
> in a list.
> However, I think I could solve my problem with the Carl's answer.
Yes, you should be able to do it with open.
Incidentally, there's a 'unique word in REBOL that may save you having
to check if your function names are in your list. It's behaviour
is...
>> unique ["a" "b" "c" "a" "c"]
== ["a" "b" "c"]
so you could just add each function name to your block as you examine
the lines then when you've finished reading the file use unique to
strip out the duplicates from the block before you sort them.
--
Carl Read
[8/9] from: richard:coffre:francetelecom at: 12-Apr-2002 11:15
Thanks a lot
-----Message d'origine-----
De : Carl Read [mailto:[carl--cybercraft--co--nz]]
Envoy=E9 : vendredi 12 avril 2002 10:17
=C0 : [rebol-list--rebol--com]
Objet : [REBOL] Re: Creating a list, writing it in a file
On 12-Apr-02, COFFRE Richard FTO wrote:
> Hi Carl and Joel
> I read your last answers but I wasn't precise enough in my query. My
<<quoted lines omitted: 6>>
> in a list.
> However, I think I could solve my problem with the Carl's answer.
Yes, you should be able to do it with open.
Incidentally, there's a 'unique word in REBOL that may save you having
to check if your function names are in your list. It's behaviour
is...
>> unique ["a" "b" "c" "a" "c"]
== ["a" "b" "c"]
so you could just add each function name to your block as you examine
the lines then when you've finished reading the file use unique to
strip out the duplicates from the block before you sort them.
--
Carl Read
[9/9] from: joel:neely:fedex at: 12-Apr-2002 6:56
Hi, Richard,
COFFRE Richard FTO wrote:
> 1. Read the line and extract (if necessary) a function name.
>
How? "if necessary" based on what? Do you mean some sort of
syntax-driven recognition/parsing?
As for the rest, let's pretend that "a function name" is the
letter #"F" followed by one or more digits.
Here's some sample data:
8<----------begin morelines.txt----------
blah blah F123 blah blah blah F45 blah blah
yadda yadda F678 yadda yadda F123 yadda F678
bloop bloop bloop F bloop
tada tada tada tada
dum da dum dum F123 F9 dum da dum dum
8<----------morelines.txt ended----------
and here's the script that does what you described:
8<----------begin morelines.r----------
REBOL []
morelines: make object! [
names: []
digits: charset "0123456789"
fnamerule: [#"F" some digits]
run: func [fi [file!] fo [file!]] [
names: copy []
foreach line read/lines fi [
parse/all line [
any [
copy name fnamerule (
if not found? find names name [
append names name
]
)
|
skip
]
]
]
write/lines fo sort unique names
]
]
8<----------morelines.r ended----------
after saying this at the console:
>> morelines/run %morelines.txt %morelines2.txt
we have
8<----------begin morelines2.txt----------
F123
F45
F678
F9
8<----------morelines2.txt ended----------
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted