Hi. I want to add a normalized column to a csv file. That is, I want to
read the file, sum all of a column X, then add another column in which
each X is divided by the sum. So do I use the CSV rows twice without
reading the file twice?

The examples in the FasterCSV documentation at
http://fastercsv.rubyforge.org/ show class methods that provide
file-like operations. I want the file read once while the data is read,
then closed. While reading the first time, the column sum is calculated.
But then I want to go through the csv rows again, this time writing out
the rows with their new column.

Here's a sketch of what I had in mind. It doesn't work as intended...

  # read csv file, summing the values
  sum = 0
  csv =
FasterCSV.new(open(csv_filename),{:headers=>true}).each_with_index do |
row, c |
    sum += row["VAL"].to_f
    end

  # now write
  FasterCSV.open("test_csv_file.csv", "w", {:headers=>true}) do |csvout|
    csv.each_with_index do | row, c |
            row["NORMED"] = row["VAL"].to_f / sum
        csvout << row.headers if c==0
        csvout << row
    end
  end

Also, is there a more graceful way to have the headers written out?

-- 
Posted via http://www.ruby-forum.com/.