> I would like to read a text file a line at
> a time. What's more, I would like to read each
> line into a variable.
> After looking at the draft manual and reading
> fillines.html, I attempted the following code:
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> file: open/read %test.txt
> foreach line file [print line]
> ;like to replace the above line of code
> ;with one that reads into a variable
> ;perhaps for parsing or modification
> ;and then prints it
> close text
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> {The second line of code generates an error message}
>

Hi Tim:

I would do it something like this:

file: read/lines %test.txt                   ; read the file line by line
into a variable
z: length? file                              ; find out how many lines there
are
x: 0                                         ; use this variable to track
lines
loop z [                                     ; set up a line by line loop
         x = x + 1                           ; specify line number in
sequence
           print pick file x                   ; print the current line number
       ]

In the loop, of course, you can add whatever you wish to manipulate each
individual lines.

You'll find REBOL elegantly simple for operations of this type.

--Ralph

Reply via email to