简体   繁体   English

在Perl中,我如何按值的频率排序?

[英]In Perl, 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,B,C,那就像是这样的。输出类似于

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 通过做这样的事情,我已经能够轻松获得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};
     }

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 - 如何通过计数而不是键(或值A,B,C)对printf的输出进行排序 -

A   456
C   344
B   234

This is a FAQ: 这是一个FAQ:

perldoc -q sort 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
for my $w (sort {$count{$b} <=> $count{$a}} keys %count) {
    print "$w\t$count{$w}\n";
}

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. split与常量空间一起使用而不是正则表达式拆分任何空白序列,比如split /\\s+/除了忽略尾随空格...这是一个常见的事情,它有这种特殊的语法。 Note that the chomp becomes unnecessary. 请注意,chomp变得不必要了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM