Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Grouping By A Column

by roboticus (Chancellor)
on Jan 27, 2011 at 02:12 UTC ( [id://884462]=note: print w/replies, xml ) Need Help??


in reply to Grouping By A Column

rocky13

You're on the right track. You do grouping with a hash. Each time you get a row, create the entry if it doesn't exist, or add in the data:

my ($col1, $col2, $col3) = generate_data(); if (! defined $hash{$col1}) { $hash{$col1} = [ 0, 0 ] } $hash{$col1}[0] += $col2; $hash{$col1}[1] += $col3;

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Grouping By A Column
by JavaFan (Canon) on Jan 27, 2011 at 11:35 UTC
    Due to autovivification, you can half the number of lines in the snippet:
    my ($col1, $col2, $col3) = generate_data(); $hash{$col1}[0] += $col2; $hash{$col1}[1] += $col3;
    will do.
Re^2: Grouping By A Column
by rocky13 (Acolyte) on Jan 27, 2011 at 04:29 UTC
    That clears up a lot of things. One more question. Can you read an excel file the same way you read a .txt file?

      In a word: 'No'. But there are many Excel related modules in CPAN. A good starting point is probably Spreadsheet::ParseExcel.

      True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://884462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found