[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

count distinct values in csv

chris

3/23/2007 9:43:00 AM

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

3 Answers

Anthony Eden

3/23/2007 11:37:00 AM

0

Consider using either the built in CSV parser in Ruby stdlib or
FasterCSV ( http://fastercsv.ruby... ). It'll save you time in
the long run.

V/r
Anthony Eden

On 3/23/07, Christian <ozric@web.de> 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?
>
>
> ## 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
>
>
>


--
Cell: 808 782-5046
Current Location: Melbourne, FL

Drew Olson

3/23/2007 2:05:00 PM

0

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-manipulati...

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-....

chris

3/23/2007 6:24:00 PM

0

many thanks , ruby is really great and i have to learn think "easy"!
christian


On 23 Mrz., 15:04, Drew Olson <olso...@gmail.com> wrote:
> 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-manipulati...
>
> 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 viahttp://www.ruby-....