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,
splitbased logic ok. complicated x-separated files, recommend usingtext::csv_xs/text::csvcpan moduledon't need initialize
%countor$wvariables - hash autoinitialized empty hash, ,$wgets assigned loop variable - may want declare in loop itself:foreach $w (sort keys %count) {please don't use 1-letter variables.
$wmeaningless in last loop, whereas$os_nameclear.
Comments
Post a Comment