Counting total sum of each value in one column w.r.t another in Perl
Posted
by
sfactor
on Stack Overflow
See other posts from Stack Overflow
or by sfactor
Published on 2011-02-17T23:08:31Z
Indexed on
2011/02/17
23:25 UTC
Read the original article
Hit count: 298
I have tab delimited data with multiple columns.
I have OS names in column 31 and data bytes in columns 6 and 7. What I want to do is count the total volume of each unique OS.
So, I did something in Perl like this:
#!/usr/bin/perl
use warnings;
my @hhfilelist = glob "*.txt";
my %count = ();
for my $f (@hhfilelist) {
open F, $f || die "Cannot open $f: $!";
while (<F>) {
chomp;
my @line = split /\t/;
# counting volumes in col 6 and 7 for 31
$count{$line[30]} = $line[5] + $line[6];
}
close (F);
}
my $w = 0;
foreach $w (sort keys %count) {
print "$w\t$count{$w}\n";
}
So, the result would be something like
Windows 100000
Linux 5000
Mac OSX 15000
Android 2000
But there seems to be some error in this code because the resulting values I get aren't as expected.
What am I doing wrong?
© Stack Overflow or respective owner