From: James Edward Gray II Date: 2007-02-06T22:35:54+09:00 Subject: Re: How do you get the rows out of FasterCSV? On Feb 5, 2007, at 11:58 PM, ara.t.howard@noaa.gov wrote: > require 'rubygems' > require 'fastercsv' > > csv = <<-csv > f,x > a,0 > b,1 > c,2 > d,3 > csv > > fcsv = FasterCSV.new csv, :headers => true > table = fcsv.read Or just: table = FCSV.parse(csv, :headers => true) > xs = table['x'].map{|x| x.to_i} When you want to convert a field, ask FasterCSV to do it for you while reading. This changes the above to: table = FCSV.parse( csv, :headers => true, :converters => lambda { |f, info| info.header == "x" ? f.to_i : f } ) Or using a built-in converter: table = FCSV.parse(csv, :headers => true, :converters => :integer) > sum = Float xs.inject{|sum,i| sum += i} This is now simplified to: sum = Float table['x'].inject{|sum,i| sum += i} > norm = xs.map{|x| x / sum} > > table['n'] = norm > puts table James Edward Gray II