sfactor
sfactor

Reputation: 13062

How do I sort by frequency of a value?

I am trying to create a program to count the different values that occur in a column of a data file. So, it would be something like, if the possible values of a column are A, B, C. The output is something like

A   456
B   234
C   344

I have been able to get the running counts of A, B and C easily 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 instance here I am counting the second column of the file in the path given.

How do I sort the output of the printf by the counts rather than the keys (or values A, B, C) to get the following output?

A   456
C   344
B   234

Upvotes: 6

Views: 1997

Answers (3)

toolic
toolic

Reputation: 62121

This is a FAQ:

perldoc -q sort

use warnings;
use strict;

my %count = (
    A => 456,
    B => 234,
    C => 344
);

for my $w (sort { $count{$b} <=> $count{$a} } keys %count) {
    print "$w\t$count{$w}\n";
}

Output:

A       456
C       344
B       234

Upvotes: 8

ysth
ysth

Reputation: 98398

Some additional comments:

The output is something like...by doing something like this

You help us help you if you paste your actual code, abbreviated where possible. When people recreate their actual code, they often obscure or omit the very source of their problem.

   chomp;
   my @U = split / /;

This splits on space characters and looks for the count after the second space; it's often easier to do:

   my @U = split ' ';

split used with a constant space instead of a regex splits on any sequence of whitespace, like split /\s+/ except that it ignores trailing whitespace...this is a common enough thing to do that there is this special syntax for it. Note that the chomp becomes unnecessary.

Upvotes: 2

Eugene Yarmash
Eugene Yarmash

Reputation: 149933

for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
    print "$w\t$count{$w}\n";
}

Upvotes: 3

Related Questions