Read File Into An Array

Hi,

I want to read a file into an array. I'm using the following code:

contentsArray = Array.new

f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = filenamehere.each_line { |line| }

However, it seems to put the whole file into the array as one item. If
I try and use contentsArray.slice! I get:

undefined method 'slice!' for #<File:filenamehere> (NoMethodError)

So it sees the contents of the array as a file instead of a list of text
lines as I want.

What am I doing wrong?

Thanks in advance.

···

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

try:

contentsArray = f.each_line {|line|}

instead.

···

On 5/23/06, bc90141 <info@libertarianswag.com> wrote:

Hi,

I want to read a file into an array. I'm using the following code:

contentsArray = Array.new

f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = filenamehere.each_line { |line| }

contents = File.readlines("somefile")

If the file does not exist Errno::ENOENT will be raised. The "or die" is not valid ruby. Try it:

true or die # => true
false or die # =>
# ~> -:2: undefined local variable or method `die' for main:Object (NameError)

-- Daniel

···

On May 23, 2006, at 4:28 PM, bc90141 wrote:

I want to read a file into an array. I'm using the following code:

contentsArray = Array.new
f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = filenamehere.each_line { |line| }

However, it seems to put the whole file into the array as one item. If
I try and use contentsArray.slice! I get:

undefined method 'slice!' for #<File:filenamehere> (NoMethodError)

So it sees the contents of the array as a file instead of a list of text
lines as I want.

array = IO.readlines pathname

regards.

-a

···

On Tue, 23 May 2006, bc90141 wrote:

Hi,

I want to read a file into an array. I'm using the following code:

contentsArray = Array.new

f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = filenamehere.each_line { |line| }

However, it seems to put the whole file into the array as one item. If
I try and use contentsArray.slice! I get:

undefined method 'slice!' for #<File:filenamehere> (NoMethodError)

So it sees the contents of the array as a file instead of a list of text
lines as I want.

What am I doing wrong?

Thanks in advance.

--
Posted via http://www.ruby-forum.com/\.

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

Madan Manoharan wrote:

···

On 5/23/06, bc90141 <info@libertarianswag.com> wrote:

Hi,

I want to read a file into an array. I'm using the following code:

contentsArray = Array.new

f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = filenamehere.each_line { |line| }

try:

contentsArray = f.each_line {|line|}

instead.

Sorry... I typed that into the forum incorrectly. I actually have
f.each_line, and it still sees it as a file in the array and not the
individual lines.

--
Posted via http://www.ruby-forum.com/\.

f = File.open("filenamehere") or die "Unable to open file..."
contentsArray = f.each_line { |line| }

The {|line|} thing after each_line is a `code block`, which is an
important concept in ruby (you'll want to read up on it...). It's like
a anonymous function that you pass as a parameter to another function.
The |line| bit is basically the specification of the parameters of the
anonymous function.

What `.each_line ` does is to call the anonymous function once for
each line in the File object. The anonymous function that you defined
does nothing. What you want it to do is push each line of the file
into the array. So try:

  contentsArray= # start with an empty array
  f.each_line {|line|
    contentArray.push line
  }

That should do it.
   -tim

···

On 5/23/06, bc90141 <info@libertarianswag.com> wrote:

Madan Manoharan wrote:
> On 5/23/06, bc90141 <info@libertarianswag.com> wrote:
>> Hi,
>>
>> I want to read a file into an array. I'm using the following code:
>>
>> contentsArray = Array.new
>>
>> f = File.open("filenamehere") or die "Unable to open file..."
>> contentsArray = filenamehere.each_line { |line| }
>>
>
> try:
>
> contentsArray = f.each_line {|line|}
>
> instead.

Sorry... I typed that into the forum incorrectly. I actually have
f.each_line, and it still sees it as a file in the array and not the
individual lines.

--
Posted via http://www.ruby-forum.com/\.

Tim Becker wrote:

  contentsArray= # start with an empty array
  f.each_line {|line|
    contentArray.push line
  }

That should do it.
   -tim

That did it - thanks! :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs. If you want to do it while iterating
yourself you should at least use the block form of File.open().

File.open your_file do |f|
  f.each_line ...
end

Kind regards

robert

···

2006/5/23, bc90141 <info@libertarianswag.com>:

Tim Becker wrote:

> contentsArray= # start with an empty array
> f.each_line {|line|
> contentArray.push line
> }
>
> That should do it.
> -tim

That did it - thanks! :slight_smile:

--
Have a look: Robert K. | Flickr

Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs. If you want to do it while iterating
yourself you should at least use the block form of File.open().

or IO::foreach:

IO.foreach("somefile") do |line|
   ...
end

anonymous ruby-forum user wrote:

That did it - thanks! :slight_smile:

f = open("somefile")
contents_array =
f.each_line { |line| contents_array << line }
f.close

or:

contents_array = IO.readlines("somefile")

Which looks better?

-- Daniel

···

On May 23, 2006, at 5:43 PM, Robert Klemme wrote:

Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs.

Of course `readlines` suits his needs, but it doesn't explain why the
original code wasn't working. Codeblocks don't exist in mainstream
programming languages which makes the concept hard to grasp for people
starting ruby. At that stage of learning it's probably more helpful to
learn why the way you're trying to it doesn't work that to hear that
there is a MUCH easier, but completely different way that does work.
On the other hand, I guess it's helpful to learn more about the
standard library as well...
   -tim

I don't accept your second variant because you didn't use the block form. :-))

robert

···

2006/5/23, Daniel Harple <dharple@generalconsumption.org>:

On May 23, 2006, at 5:43 PM, Robert Klemme wrote:

> Why do you settly with a complicated version? IO.readlines() will
> perfectly suit your needs. If you want to do it while iterating
> yourself you should at least use the block form of File.open().

or IO::foreach:

IO.foreach("somefile") do |line|
   ...
end

anonymous ruby-forum user wrote:
> That did it - thanks! :slight_smile:

f = open("somefile")
contents_array =
f.each_line { |line| contents_array << line }
f.close

or:

contents_array = IO.readlines("somefile")

Which looks better?

--
Have a look: Robert K. | Flickr