Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: group by and sum for two columns

by kcott (Archbishop)
on Aug 09, 2017 at 09:40 UTC ( [id://1197090]=note: print w/replies, xml ) Need Help??


in reply to group by and sum for two columns

G'day gowthamvels,

What you're after is a very simple exercise in using Text::CSV. Here's the guts of it:

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; use Text::CSV; my $csv = Text::CSV::->new; my %result; while (my $row = $csv->getline(\*DATA)) { $result{$row->[1]}[0] += $row->[6]; $result{$row->[1]}[1] += $row->[7]; } dd \%result; __DATA__ 1234,GP,20170715,0,V,97517,24,0.6 5678,Pack,20170715,0,V,97516,88,1.8 1234,GP,20170715,0,V,97517,22,0.6 5678,Pack,20170715,0,V,97517,183,3.9 1234,PRS,20170715,0,S,97517,261,5.4 5678,PRS,20170715,0,M,97517,36,0.9

Output:

{ GP => [46, 1.2], Pack => [271, 5.7], PRS => [297, 6.3] }

Please post data between <code>...</code> tags (as already requested in responses to this and your last post). Show the data as it appears in the file: do not make records into dot points or otherwise change their original format. Also check what you post, multiple times if necessary, using the "preview" button: in this instance, you posted "col1,...,col7" but there are eight columns, not seven.

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found