From: Robert Klemme Date: 2003-04-24T17:47:10+09:00 Subject: Re: parsing large file into matrix "Joel VanderWerf" 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 # read matrix=[] File.open("file.txt").each{|line| matrix << ( line.chomp.split("\t").map {|s|s.to_f} ) # alternative use split( /\s+/ ) to use all white space as separator } # write again matrix.each{|x| puts x.join "\t"} robert