I am trying to create a program to count the various values that are found in a column of a data file. So this would be something like if the possible column values are: A, B, C. The output looks like
A 456
B 234
C 344
I was able to easily do the calculations of A, B and C by doing something like this
my %count;
for my $f (@ffile) {
open F, $f || die "Cannot open $f: $!";
while (<F>) {
chomp;
my @U = split / /;
$count{$U[2]}++;
}
}
foreach my $w (sort keys %count) {
printf $w\t$count{$w};
}
For example, here I am counting the second column of a file in the specified path.
How to sort printf output using counters, not keys (or values A, B, C) to get -
A 456
C 344
B 234
source
share