I work on a lot of flat files containing data with many columns
and thousands of rows. I have recently started using ruby, in
hopes to forgo the overworking of sed and awk or tedious coding of C.
So far I like it.
Question: It would be ideal if I could open a file, read in the data
and then start working on the resulting array by a [m,n] indexing. Thus
far using Ruby I can do this fairly quickly:
x = File.open(“file.txt”).readlines
j = 0
ffsize = Array.new
name = Array.new
while j < x.length
y = x[j].split(’\t’)
ffsize[j] = y.shift.to_f/1024.0
name[j] = y.shift
j += 1
end
Currently I am just using a small dummy file as a test, but this will become
huge once I start using files with many columns of data and I will likely
join these field arrays together again.
Is there a Ruby-way of creating an m by n array from the beginning?
I know the following doesn’t work but something like it code would be nice:
x = File.open(“file.txt”).readlines.split(’\t’)
Then x would be an m by n array of m different data types.
Is there a Ruby-way of creating an m by n array from the beginning?
I know the following doesn’t work but something like it code would be nice:
x = File.open(“file.txt”).readlines.split(‘\t’)
How about this?
x = File.open(“file.txt”).readlines.
map {|line| line.chomp.split(“\t”).
map {|s|s.to_f}}
Note that “\t” is necessary so that you get an actual tab char.
x = File.open(“file.txt”).readlines.collect! do |line|
line.chomp.split(“\t”)
end
Although that leaves the file open until garbage collection time. The
following does not have that problem; it also processes the lines as they
are read.
x = File.open(“file.txt”) do |f|
f.collect do |line|
line.chomp.split(“\t”)
end
end
Regards,
Brian.
···
On Thu, Apr 24, 2003 at 02:26:08PM +0900, Qubert wrote:
Is there a Ruby-way of creating an m by n array from the beginning?
I know the following doesn’t work but something like it code would be nice:
x = File.open(“file.txt”).readlines.split(‘\t’)
“Joel VanderWerf” vjoel@PATH.Berkeley.EDU schrieb im Newsbeitrag
news:3EA77CED.8010201@path.berkeley.edu…
Qubert wrote:
Is there a Ruby-way of creating an m by n array from the beginning?
I know the following doesn’t work but something like it code would be
nice:
x = File.open(“file.txt”).readlines.split(‘\t’)
How about this?
x = File.open(“file.txt”).readlines.
map {|line| line.chomp.split(“\t”).
map {|s|s.to_f}}
I don’t think this closes the file properly. And, another disadvantage, it
keeps the whole file in memory. How about
Is there a Ruby-way of creating an m by n array from the beginning?
I know the following doesn’t work but something like it code would be
nice:
x = File.open(“file.txt”).readlines.split(‘\t’)
x = File.open(“file.txt”).readlines.collect! do |line|
line.chomp.split(“\t”)
end
Although that leaves the file open until garbage collection time. The
following does not have that problem; it also processes the lines as
they
are read.
x = File.open(“file.txt”) do |f|
f.collect do |line|
line.chomp.split(“\t”)
end
end
Thanks for correcting me. That was what I intended. What bugs me is that
documentation says that File.open() with a block returns nil. But
apparently it works. So with the additional conversion to float we get
matrix = File.open(“file.txt”) do |f|
f.collect do |line|
line.chomp.split(“\t”).map {|s|s.to_f}
end
end
Cheers
robert
···
On Thu, Apr 24, 2003 at 02:26:08PM +0900, Qubert wrote: