From: Drew Olson Date: 2007-03-23T23:04:54+09:00 Subject: Re: count distinct values in csv Christian Schulz wrote: > Hi, > > i'm new to ruby. > How could i read a tab-delimted textfile with two columns + header > row > and count the distinct values from the 2.column? Christian - Strangely enough, I wrote a blog post about this very topic: http://drewolson.wordpress.com/2007/03/13/csv-manipulation-w-ruby/ Here's the code linked to by the blog post: require 'faster_csv' unique_count = {} FCSV.foreach("myfile.csv", :headers => true) do |row| unique_count[row[1]] ||= 0 unique_count[row[1]] += 1 end unique_count.each do |val,count| puts "#{val} appreas #{count} time(s)" end -- Posted via http://www.ruby-forum.com/.