sfa*_*tor 5 sorting perl key count
我正在尝试创建一个程序来计算数据文件列中出现的不同值.所以,如果列的可能值是A,B,C,那就像是这样的.输出类似于
A 456
B 234
C 344
Run Code Online (Sandbox Code Playgroud)
通过做这样的事情,我已经能够轻松获得A,B和C的运行计数
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};
}
Run Code Online (Sandbox Code Playgroud)
例如,我在计算给定路径中的文件的第二列.
如何通过计数而不是键(或值A,B,C)对printf的输出进行排序 -
A 456
C 344
B 234
Run Code Online (Sandbox Code Playgroud)
这是一个FAQ:
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
Run Code Online (Sandbox Code Playgroud)
for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
print "$w\t$count{$w}\n";
}
Run Code Online (Sandbox Code Playgroud)