From: 7stud -- Date: 2007-11-14T04:56:27+09:00 Subject: Re: reading a column formatted data 7stud -- wrote: > > require 'rubygems' > require 'fastercsv' > > totals = {'Poor' =>[0,0,0], 'Fair'=>[0,0,0], 'Excellent'=>[0,0,0]} > cols = totals.length > > FasterCSV.foreach('data.txt', :headers =>true) do |row| > cols.times do |i| > totals[row[i+1]][i] += 1 > end > end > > p totals > > --output:-- > {"Excellent"=>[2, 2, 1], "Poor"=>[2, 1, 1], "Fair"=>[3, 4, 5]} This is more readable: require 'rubygems' require 'fastercsv' totals = {'Poor' =>[0,0,0], 'Fair'=>[0,0,0], 'Excellent'=>[0,0,0]} target_cols = ['Q01', 'Q02', 'Q03'] FasterCSV.foreach('data.txt', :headers =>true, :col_sep =>" ") do |row| target_cols.each_with_index do |col_name, i| rating = row[col_name] totals[rating][i] += 1 end end p totals --output:-- {"Excellent"=>[2, 2, 1], "Poor"=>[2, 1, 1], "Fair"=>[3, 4, 5]} -- Posted via http://www.ruby-forum.com/.