[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Faster CSV,need help with logic

Nick Barba

6/11/2009 5:40:00 PM

I'm having a tough time figuring out how to go about solving a specific
problem.

I have a csv file that looks like this:

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

I need to figure out how to consolidate the rows so that there is just
one row per name, adding any actual hours together, and leaving the
latest date.

So that would become:

Name,Estimated Hours,Actual Hours,Date
Casey,200,200,2009-04-30
Avino,100,100,2009-05-31
Black,30,25,2009-05-31
Clothier,80,90,2009-05-31

Anyone have any ideas? I was thinking I would need to start by looking
at the first line, then scan the rest of the file for rows that have
matching names and store all of those. Then write a csv file with just
that new combined line, and then delete all rows with those names. Then
move onto the next name and do the same thing. Problem is I can't seem
to figure out how to code it...
--
Posted via http://www.ruby-....

4 Answers

snex

6/11/2009 5:43:00 PM

0

On Jun 11, 12:40 pm, Nick Barba <nickba...@gmail.com> wrote:
> I'm having a tough time figuring out how to go about solving a specific
> problem.
>
> I have a csv file that looks like this:
>
> 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
>
> I need to figure out how to consolidate the rows so that there is just
> one row per name, adding any actual hours together, and leaving the
> latest date.
>
> So that would become:
>
> Name,Estimated Hours,Actual Hours,Date
> Casey,200,200,2009-04-30
> Avino,100,100,2009-05-31
> Black,30,25,2009-05-31
> Clothier,80,90,2009-05-31
>
> Anyone have any ideas?  I was thinking I would need to start by looking
> at the first line, then scan the rest of the file for rows that have
> matching names and store all of those. Then write a csv file with just
> that new combined line, and then delete all rows with those names.  Then
> move onto the next name and do the same thing.  Problem is I can't seem
> to figure out how to code it...
> --
> Posted viahttp://www.ruby-....

store everything in a hash table with the name as the key. if the key
exists, add the hours and replace the date if necessary, otherwise
insert it with the data given.

James Gray

6/11/2009 7:17:00 PM

0

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

Nick Barba

6/11/2009 7:53:00 PM

0

Wow that was it exactly! Thanks for the FasterCSV gem too James!
--
Posted via http://www.ruby-....

RLunfa

6/24/2010 6:04:00 PM

0

"Alboroto" <Alboroto@sincorreo.com> escribi? en el mensaje
news:i004h1$mug$2@news.eternal-september.org...
| RLunfa escribi?:
| > "PINKO" <ngsemail2005withoutthis@yahoo.com.ar> escribi? en el mensaje
| > news:hvsv88$m5s$1@news.eternal-september.org...
| > | Buenisima imitacion de Maradona. Hay que admitir que los argentos
producen
| > | personajes publicos desde Evita a Maradona que son impagables.
| > | http://www.ciudad.com.ar/2010/06/22/actualidad/022...
| > | PINKO
| >
| > Espantoso.
| >
| > Es algo as? como Pinke intentando parecer argentino.
| >
| > RLunfa
|
| Pero es gracioso, hombre. Adem?s, es envidia de no tener personas que se
| salgan tanto de la norma, como Argentina.

Si, es verdad. Pinke es gracioso.
Pero no es argentino.

RLunfa