Reputation: 613
Not sure if that's even the right title for this question as I'm new to Perl, but I have a text file with 2 columns of interest:
AB Volume
100 280
137 250
150 375
100 100
100 600
137 200
And I want to sum up Volumes based on AB#, the resulting output being
AB Instances Volume
100 3 980
137 2 450
150 1 375
All I've done till now is to display distinct AB's in the output file, but I'm struggling to get the sum of Volume counts.
$isAB{$AB} = 1;
$isVolume{$Volume} =1;
$numAB{$AB}++;
print "AB\tInstances\tVolume\n";
for $AB (sort {$a<=>$b;} keys %numAB) {
print "$AB\t$numAB{$AB}\n";
}
Any help will be appreciated! Thanks
Upvotes: 3
Views: 2340
Reputation: 29854
Welcome to an expressive language. For something like this, I recommend List::Pairwise
.
my %sums;
List::Pairwise::mapp { $sums{ $a } += $b } %numAB;
Upvotes: 0
Reputation: 913
I recomend using record like data structure
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.010;
my %res;
while(<DATA>) {
(my $key, my $volume)= split;
$res{$key}->{QUANTITY}++;
$res{$key}->{VOLUME}+=$volume;
}
#use Data::Dumper;
#print Dumper(%res);
for my $key (sort {$a<=>$b} keys %res){
my $quantity=$res{$key}->{QUANTITY};
my $volume=$res{$key}->{VOLUME};
say join("\t",$key, $quantity,$volume);
}
__DATA__
100 280
137 250
150 375
100 100
100 600
137 200
Upvotes: 0
Reputation: 91430
How about:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my %res;
while(<DATA>) {
chomp;
my @fields = split;
$res{$fields[0]}{instance}++;
$res{$fields[0]}{volume} += $fields[1];
}
foreach(sort {$a<=>$b} keys(%res)) {
say "$_\t$res{$_}{instance}\t$res{$_}{volume}";
}
__DATA__
100 280
137 250
150 375
100 100
100 600
137 200
output:
100 3 980
137 2 450
150 1 375
Upvotes: 6
Reputation: 561
Add another hash to keep track of the sum
$sumAB{$AB} += $isAB{$AB};
then in your print loop
print "$AB\t$numAB{$AB}\t$sumAB{$AB}\n";
Upvotes: 1
Reputation: 36262
One way:
Content of infile
:
AB Volume
100 280
137 250
150 375
100 100
100 600
137 200
Content of script.pl
:
use warnings;
use strict;
use List::Util qw( sum );
## Check arguments.
die qq[Usage: perl $0 <input-file>\n] unless @ARGV == 1;
## Hash to save content of input file.
my (%ab);
while ( <> ) {
## Split line. If number of fields is different from two, omit it
## and read next one.
my @f = split;
next unless @f == 2;
## In first line print header.
if ( $. == 1 ) {
printf qq[%s\n], join qq[\t], $f[0], qq[Instances], $f[1];
next;
}
## Save fields of line.
push @{ $ab{ $f[0] } }, $f[1];
}
## Print to output.
for ( sort { $a <=> $b } keys %ab ) {
printf qq[%s\t%s\t%s\n], $_, scalar @{ $ab{ $_ } }, sum @{ $ab{ $_ } };
}
Run the script:
perl script.pl infile
And output:
AB Instances Volume
100 3 980
137 2 450
150 1 375
Upvotes: 2