From: "David A. Black" Date: 2008-06-19T04:12:42+09:00 Subject: Re: parsing text into usablle numerical data Hi -- On Thu, 19 Jun 2008, Chris Hulan wrote: > On Jun 18, 2:10 pm, Cthulhu __ wrote: >> Still having some problems with this program. I'm getting a TypeError >> when the value of >> >> dx, *dat = line.scan(/\d+/).map.compact >> is nil >> >> Any suggestions on how to handle this? my current implementation looks >> like this: >> >> raw_data = Array.new >> File.foreach files_to_parse[i].to_s do |line| >> idx, *dat = line.scan(/\d+/).map.compact! {|s| s.to_i} >> raw_data[idx] = dat >> end > ... > > looks like the blank line between records is the culprit > could just skip it explicitly: > >> File.foreach files_to_parse[i].to_s do |line| > next if line.strip == '' #skip line if it is empty >> idx, *dat = line.scan(/\d+/).map.compact! {|s| s.to_i} >> raw_data[idx] = dat >> end That scan/map/compact! line is still wrong. The block should go with map, not compact!, and compact! returns nil if its receiver doesn't change: [1,2,3].compact! # => nil There's no reason that line.scan(/\d+/) would ever contain nil, so compact! will always return nil and map, in that position, does nothing in 1.8 and makes compact! blow up in 1.9 :-) David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS June 16-19 Berlin ADVANCING WITH RAILS July 21-24 Edison, NJ See http://www.rubypal.com for details and updates!