parsing - Counting total sum of each value in one column w.r.t another in Perl -
i have tab delimited data multiple columns.
i have os names in column 31 , data bytes in columns 6 , 7. want count total volume of each unique os.
so, did in perl this:
#!/usr/bin/perl use warnings; @hhfilelist = glob "*.txt"; %count = (); $f (@hhfilelist) { open f, $f || die "cannot open $f: $!"; while (<f>) { chomp; @line = split /\t/; # counting volumes in col 6 , 7 31 $count{$line[30]} = $line[5] + $line[6]; } close (f); } $w = 0; foreach $w (sort keys %count) { print "$w\t$count{$w}\n"; }
so, result like
windows 100000 linux 5000 mac osx 15000 android 2000
but there seems error in code because resulting values aren't expected.
what doing wrong?
it seems you're not adding counts - overwrite last count os count last line os.
$count{$line[30]} = $line[5] + $line[6];
should be
$count{$line[30]} += $line[5] + $line[6];
as additional considerations can improve code overall don't affect correctness of it:
please use 3-argument form of open , lexical filehandles:
open(my $filehandle, "<", $f) || die "cannot open $f: $!";
if you're 100% sure file doesn't contain quoted field values or tabs in field contents,
split
based logic ok. complicated x-separated files, recommend usingtext::csv_xs
/text::csv
cpan moduledon't need initialize
%count
or$w
variables - hash autoinitialized empty hash, ,$w
gets assigned loop variable - may want declare in loop itself:foreach $w (sort keys %count) {
please don't use 1-letter variables.
$w
meaningless in last loop, whereas$os_name
clear.
Comments
Post a Comment