您可以处理文件两次:第一次运行只计算行数 ( $.
)。根据该数字,您可以计算滑动窗口的大小,它只会保留找到百分位数所需的最高数字(对于百分位数 < 50,您应该反转逻辑)。
#!/usr/bin/perl
use warnings;
use strict;
my $percentile = 95;
my $file = shift;
open my $IN, '<', $file or die $!;
1 while <$IN>; # Just count the number of lines.
my $line_count = $.;
seek $IN, 0, 0; # Rewind.
# Calculate the size of the sliding window.
my $remember_count = 1 + (100 - $percentile) * $line_count / 100;
# Initialize the window with the first lines.
my @window = sort { $a <=> $b }
map scalar <$IN>,
1 .. $remember_count;
chomp @window;
while (<$IN>) {
chomp;
next if $_ < $window[0];
shift @window;
my $i = 0;
$i++ while $i <= $#window and $window[$i] <= $_;
splice @window, $i, 0, $_;
}
print "$window[0]\n";