#!/usr/bin/perl use strict; use warnings; use autodie; use feature ':5.10'; my $top_N = 10; my $gen_score = sub { 1000 * rand() * rand () }; my $noisy = 0; # 0=quiet, 1=show repl, 2=show add, 3=all traces # Generate temp data if (@ARGV or ! -e 'TEMP') { my $row_count = shift // 100; say "Building temp file with $row_count items" if $noisy; open my $FH, '>', 'TEMP'; printf $FH "ID_%04u %.2f\n", $_, &$gen_score() for 1 .. $row_count; close $FH; } my @top; my ($cnt_add, $cnt_replace) = (0)x2; open my $FH, '<', 'TEMP'; while (<$FH>) { my ($id, $score) = split ' '; if (@top<$top_N) { @top = sort { $a->[1] <=> $b->[1] } @top, [ $id, $score]; say "$.: [$id, $score] ADD => ", scalar(@top), " $top[0][0]:$top[0][1] .. $top[-1][0]:$top[-1][1]" if $noisy > 1; ++$cnt_add; } elsif ($score > $top[0][1]) { @top = sort { $a->[1] <=> $b->[1] } [$id, $score], @top[1 .. $#top]; say "$.: [$id, $score] REPL => ", scalar(@top), " $top[0][0]:$top[0][1] .. $top[-1][0]:$top[-1][1]" if $noisy; ++$cnt_replace; } else { say "$.: [$id, $score] < [ $top[0][0], $top[0][1] ]" if $noisy > 2; } } for (@top) { say "$_->[0] : $_->[1]"; } say "adds: $cnt_add, replacements: $cnt_replace, lines: $."; #### $ perl 919076.pl 1500 ID_0762 : 897.90 ID_1218 : 898.96 ID_1468 : 917.94 ID_0195 : 920.68 ID_0089 : 921.92 ID_0071 : 925.55 ID_0668 : 933.69 ID_1425 : 940.34 ID_0374 : 962.16 ID_0185 : 984.86 adds: 10, replacements: 45, lines: 1500