[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to import text file and split it up by lines

Kristen

7/2/2007 1:58:00 AM

Hello,

Im attempting to analyze a text file, but having trouble splitting it up
into lines (into an array that indexes each line). Then I would like to
print every line.

Here is what I got:

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.split("\n")

puts f_lines


And here is the error Im getting:
NoMethodError: private method â??splitâ?? called for
#<File:/Users/john/Desktop/text.txt>


Dont know why this isnt working. Should be simple enough...

Can someone help please?

--
Posted via http://www.ruby-....

4 Answers

Vasco Andrade e silva

7/2/2007 2:04:00 AM

0

Al Cholic wrote:
> Hello,
>
> Im attempting to analyze a text file, but having trouble splitting it up
> into lines (into an array that indexes each line). Then I would like to
> print every line.
>
> Here is what I got:
>
> f = File.open("/Users/john/Desktop/text.txt")
>
> f_lines = f.split("\n")
>
> puts f_lines
>
>
> And here is the error Im getting:
> NoMethodError: private method â??splitâ?? called for
> #<File:/Users/john/Desktop/text.txt>
>
>
> Dont know why this isnt working. Should be simple enough...
>
> Can someone help please?
Hi,

you forgot to 'read' file.

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.read.split("\n")

puts f_lines

Note: you have also 'each_line' in IO, IOString, String, etc.

Vasco Andrade e Silva

--
Posted via http://www.ruby-....

Chris Shea

7/2/2007 2:36:00 AM

0

On Jul 1, 7:58 pm, Al Cholic <desert...@hot.ee> wrote:
> Hello,
>
> Im attempting to analyze a text file, but having trouble splitting it up
> into lines (into an array that indexes each line). Then I would like to
> print every line.
>
> Here is what I got:
>
> f = File.open("/Users/john/Desktop/text.txt")
>
> f_lines = f.split("\n")
>
> puts f_lines
>
> And here is the error Im getting:
> NoMethodError: private method 'split' called for
> #<File:/Users/john/Desktop/text.txt>
>
> Dont know why this isnt working. Should be simple enough...
>
> Can someone help please?
>
> --
> Posted viahttp://www.ruby-....

File.readlines() does everything you need. You don't even need to use
File.new or File.open.

f_lines = File.readlines('/Users/john/Desktop/test.txt')

HTH,
Chris

Robert Dober

7/2/2007 6:17:00 AM

0

On 7/2/07, Vasco Andrade e Silva <vascoas@gmail.com> wrote:
>
> f = File.open("/Users/john/Desktop/text.txt")
>
> f_lines = f.read.split("\n")

There is a convenient shortcut
File.readlines("/User/....")
>
HTH
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Kristen

7/2/2007 7:20:00 AM

0

> f_lines = f.read.split("\n")

Thanks man!

--
Posted via http://www.ruby-....