From: suresh Date: 2007-11-15T13:50:00+09:00 Subject: Re: reading a column formatted data hi, thanks for the wonderful ideas, suresh India On Nov 14, 12:56 am, 7stud -- wrote: > 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 viahttp://www.ruby-forum.com/.