From: Robert Klemme Date: 2003-04-24T19:08:17+09:00 Subject: Re: parsing large file into matrix "Brian Candler" schrieb im Newsbeitrag news:20030424085535.GA33201@uk.tiscali.com... > 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') > > 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