James Gray
12/18/2006 10:21:00 PM
On Dec 18, 2006, at 4:04 PM, Drew Olson wrote:
> What I want to do is read in a CSV file and produce an output which
> lists the unique values from each column in the following format:
>
> Column: ColHeader1
> UniqueVal1
> UniqueVal2
> Column: ColHeader2
> UniqueVal1
> UniqueVal2
> ...
Well, if it all fits in memory it's super easy using FCSV's Tables:
#!/usr/bin/env ruby -w
require "rubygems"
require "faster_csv"
table = FCSV.parse(DATA.read, :headers => true)
table.by_col!.each do |header, col|
puts "#{header}:"
puts " #{col.uniq.join(', ')}"
end
__END__
nums,letters
1,a
2,b
2,b
3,c
3,c
3,c
James Edward Gray II