James Gray
6/11/2009 7:17:00 PM
On Jun 11, 2009, at 12:40 PM, Nick Barba wrote:
> I'm having a tough time figuring out how to go about solving a
> specific
> problem.
How about something like this?
#!/usr/bin/env ruby -wKU
require "rubygems"
require "faster_csv"
data = FCSV.parse( DATA.read, :headers => true,
:header_converters => :symbol,
:return_headers => true )
FCSV { |csv| csv << data[0].fields }
data[:name].uniq.each do |name|
next if name == "Name"
matches = data.select { |row| row[:name] == name }
FCSV { |csv| csv << [ name,
matches.first[:estimated_hours],
matches.map { |row| row[:actual_hours] }.
inject(0) { |sum, n| sum + n.to_i },
matches.map { |row|
row[:date] }.sort.reverse.first ] }
end
__END__
Name,Estimated Hours,Actual Hours,Date
Black, 30, 10, 2009-03-31
Black,30,10,2009-04-30
Casey,200,200,2009-04-30
Clothier,80,40,2009-04-30
Avino,100,100,2009-05-31
Black,30,5,2009-05-31
Clothier,80,50,2009-05-31
Hope that helps.
James Edward Gray II