Forum: Ruby count distinct values in csv

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F843639d20fdac04a5cc72d4716638a5?d=identicon&s=25 Christian Schulz (ozric)
on 2007-03-23 10:46
(Received via mailing list)
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?


## my first experiments missed because i have  to elimnate the header
row!?#######

filename = gets.chomp
text = String.new
File.open(filename) { |f|  text = f.read }
values = text.split(/\t/)
freqs = Hash.new(0)
values.each { |values| freqs[values] += 1 }
freqs = freqs.sort_by {|x,y| y }
freqs.each {|values, freq| puts values+' '+freq.to_s}


Many thanks for a starting point.
regards, christian
31254903db793bf6f84bbd607fe092fd?d=identicon&s=25 Anthony Eden (Guest)
on 2007-03-23 12:38
(Received via mailing list)
Consider using either the built in CSV parser in Ruby stdlib or
FasterCSV ( http://fastercsv.rubyforge.org/ ). It'll save you time in
the long run.

V/r
Anthony Eden
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-03-23 15:04
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-mani...

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
F843639d20fdac04a5cc72d4716638a5?d=identicon&s=25 Christian Schulz (ozric)
on 2007-03-23 19:26
(Received via mailing list)
many thanks , ruby is really great and i have to learn think "easy"!
christian
This topic is locked and can not be replied to.