use*_*782 0 sorting perl numeric
我不确定为什么这个Perl排序不起作用.请建议如何解决此问题.
while (<>) {
chomp;
if (/VIOLATE/) {
@lines = split " ", $_;
#print "$lines[-2]\n"; ## Print last but one column
my @viol = "$lines[-2]\n";
@sorted = sort {$a <=> $b} @viol;
print "@sorted";
}
};
Run Code Online (Sandbox Code Playgroud)
命令: perl test.pl test.log
test.log:
0.98 2.04 -1.106 VIOLATE
0.98 2.04 3.06
0.98 2.04 -11.06 VIOLATE
0.98 2.04 -1.06 VIOLATE
0.98 2.04 1.06
0.98 2.04 -0.226 VIOLATE
0.98 2.04 -2.06 VIOLATE
Run Code Online (Sandbox Code Playgroud)
您是否尝试匹配其中的任何行VIOLATE,将结果放入数组然后对所有违规进行排序?如果是这样,你需要@viol在循环外声明和排序:
use strict;
use warnings; # Don't forget these!
my @viol;
while (<>) {
chomp;
if (/VIOLATE/) {
my @lines = split(/\s+/); # Split on one or more whitespace characters.
push @viol, $lines[-2];
}
}
# sort and print
my @sorted = sort {$a <=> $b} @viol;
print "@sorted";
Run Code Online (Sandbox Code Playgroud)
这输出: -11.06 -2.06 -1.106 -1.06 -0.226
| 归档时间: |
|
| 查看次数: |
789 次 |
| 最近记录: |