In Perl, how do I sort by frequency values?

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
+5
source share
3 answers
for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
    print "$w\t$count{$w}\n";
}
+3
source

This is the 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";
}

__END__
A       456
C       344
B       234
+8
source

:

- - ... -

, , , . , .

   chomp;
   my @U = split / /;

; :

   my @U = split ' ';

split , split /\s+/, , ... , , . , chomp .

+2

All Articles