From: Jim Freeze Date: 2003-04-25T13:14:41+09:00 Subject: Re: parsing large file into matrix 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) } } -- Jim Freeze ---------- "It's Fabulous! We haven't seen anything like it in the last half an hour!" -- Macy's