From: Robert Klemme Date: 2008-06-19T17:29:59+09:00 Subject: Re: parsing text into usablle numerical data 2008/6/18 Cthulhu __ : > Sot he code as taken from Chris' post returns this error: > > read_data.rb:72:in `[]=': no implicit conversion from nil to integer > (TypeError) > from read_data.rb:72 > from read_data.rb:69:in `foreach' > from read_data.rb:69 > > The code looks like this with line numbers... > > 69 File.foreach files_to_parse[i].to_s do |line| > 70 next if line.strip == '' > 71 idx, *dat = line.scan(/\d+/).map {|s| s.to_i} > 72 raw_data[idx] = dat > 73 end > 74 puts raw_data > > I suspect it may be a problem in the formatting of the data... perhaps > there is some way to remove any non-integer characters eg. delimiting > chars before parsing? Your condition for next is pretty weak because it will let all lines pass which contain some garbage. Rather do this data = [] File.foreach "data.txt" do |line| idx, *dat = line.scan(/\d+/).map {|s| s.to_i} data[idx] = dat if idx end or # replace 42 with the number you need data = [] File.foreach "data.txt" do |line| idx, *dat = line.scan(/\d+/).map {|s| s.to_i} data[idx] = dat if idx && dat.length == 42 end or even data = [] File.foreach "data.txt" do |line| (idx, *dat = line.scan(/\d+/).map {|s| s.to_i}).empty? or data[idx] = dat end Kind regards robert -- use.inject do |as, often| as.you_can - without end