"chomping" an array at read time

V

Victor Reyes

------=_Part_38579_2832550.1136828642687
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Team,

ruby -v =3D=3D=3D ruby 1.8.2 (2004-12-25) [powerpc-aix4.3.3.0]

Please don't laugh at my simplistic coding "techniques?"


I am reading a file which contains 1041 one token records into an array:

f_lus =3D File.open("/file_name", "r")

lus =3D Array.new
lus =3D File.read("/file_name")

When I print the size of the array lus it shows: 14377.

So I figured that this has to do with the end of line chars at the end of
each record/token. However, if you have 1 nl char per record the size shoul=
d
be twice the number of records or 2082 and not 14,377.

I went ahead and created a new array as follows, from the old array in the
same script:

i =3D 0
newArray =3D Array.new
lus.each do |lu|
newArray =3D lu
i +=3D 1
end

Now when I puts the size of array newArray it prints 1041 correctly.

What is happening here, please?
Second, is there a way to chomp a record as it is read into the array?

Thank you

Victor

------=_Part_38579_2832550.1136828642687--
 
E

Ezra Zygmuntowicz

Team,

ruby -v === ruby 1.8.2 (2004-12-25) [powerpc-aix4.3.3.0]

Please don't laugh at my simplistic coding "techniques?"


I am reading a file which contains 1041 one token records into an
array:

f_lus = File.open("/file_name", "r")

lus = Array.new
lus = File.read("/file_name")

Right here you are createing a new arrau called lus. then you destroy
it and overwrite it with a file handle. Please try this instead:

ezra:~ ez$ cat test.txt
12
324
46
23
dgs
fh
asf
fh
dsg

lus = File.open("test.txt") {|f| f.readlines}

p lus
#=> ["12\n", "324\n", "46\n", "23\n", "dgs\n", "fh\n", "asf\n", "fh
\n", "dsg\n"]



Cheers-
Ezra
 
E

Ezra Zygmuntowicz

Or just:

lus = File.readlines("test.txt")

James-

When you do that does it close the file handle as well? I was just
using the block to make sure the file handle gets closed. Are there
certain actions that will close the handle by themselves and others
that won't?

Thanks-
-Ezra
 
V

Victor Reyes

------=_Part_41448_12754192.1136840119992
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thank you both.

Victor

James-

When you do that does it close the file handle as well? I was jus= t
using the block to make sure the file handle gets closed. Are there
certain actions that will close the handle by themselves and others
that won't?

Thanks-
-Ezra

------=_Part_41448_12754192.1136840119992--
 
J

James Edward Gray II

James-

When you do that does it close the file handle as well? I was just
using the block to make sure the file handle gets closed. Are there
certain actions that will close the handle by themselves and others
that won't?

Well, there's no way I know of to get at the file handle used here,
so I sure hope it's closed. I would argue that it's a bug if it
isn't. :)

James Edward Gray II
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top