From: Robert Klemme Date: 2003-04-25T19:18:40+09:00 Subject: Re: parsing large file into matrix "Jim Freeze" schrieb im Newsbeitrag news:20030425002319.A47026@freeze.org... > On Thursday, 24 April 2003 at 19:08:17 +0900, Robert Klemme wrote: > > > > matrix = File.open("file.txt") do |f| > > f.collect do |line| > > line.chomp.split("\t").map {|s|s.to_f} > -----------------------^^^^ > /\t/ is faster > > end > > end > > Here's a common scenario that I use. > This file allows comments that start with '#'. > All other lines that are not empty are data. > > def to_number(str) > Integer(str) rescue Float(str) rescue str > end > > matrix = [] > File.foreach(file) { |line| > next if /^\s*$/ =~ line > next if /^\s*#/ =~ line > matrix << line.strip.split(/\t/).collect { |i| to_number(i) } > } next if /^\s*(#.*)?$/ =~ line sorry, couldn't resist. :-) robert