http://qs321.pair.com?node_id=1196147


in reply to perl group by and sort from a csv input file

Hi gowthamvels,

one typical way is to use a hash as an accumulator. The hash keys should be the two first values of your CSV input, and the hash values should accumulate the third value of your CSV.

At the end, you can just print out the hash in your desired format.

For example, something like this:

use strict; use warnings; use feature 'say'; my %hash; while (<DATA>) { chomp; my ($id, $num, $val) = split /,/, $_; $hash{"$id,$num"} += $val; } for my $key (sort keys %hash) { say "$key,$hash{$key}"; } __DATA__ 3211111,100,3.2 3211112,101,3.2 3211111,100,1.2 3211112,100,2.2 3211113,100,5.2 3211112,100,0.3