[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

extract lines from text file and place in an array

Chris Ashton

10/16/2008 11:56:00 AM

hi i was wondering if anyone cud help

i have this code to read from a text file

def readftxt
in=IO.read("input.txt")

end

how could i adapt it so if multiple lines are entered it could place
each line in an array?

thanks for any help
--
Posted via http://www.ruby-....

1 Answer

Jesús Gabriel y Galán

10/16/2008 12:11:00 PM

0

On Thu, Oct 16, 2008 at 1:56 PM, Roger Reeks
<christopherashton@hotmail.com> wrote:
> hi i was wondering if anyone cud help
>
> i have this code to read from a text file
>
> def readftxt
> in=IO.read("input.txt")
>
> end
>
> how could i adapt it so if multiple lines are entered it could place
> each line in an array?

Try IO#readlines:

irb(main):001:0> IO.read("test.csv")
=> "z|x|c|v|b|n\na|b|c|d|e|f\n1|699|3|4|5|6\nq|w|e|r|t|y\n"
irb(main):002:0> IO.readlines("test.csv")
=> ["z|x|c|v|b|n\n", "a|b|c|d|e|f\n", "1|699|3|4|5|6\n", "q|w|e|r|t|y\n"]

Note that the \n is part of each string.

Jesus.