ファイルを 2 回処理できます。最初の実行では、行数のみをカウントします ( $.
)。その数から、パーセンタイルを見つけるために必要な最大数のみを保持するスライディング ウィンドウのサイズを数えることができます (パーセンタイルが 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";