extract lines from text file and place in an array

R

Roger Reeks

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
 
J

Jesús Gabriel y Galán

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top