#! perl -slw use strict; sub bestN { my( $cmp, $n, $ref ) = @_; my $src = $ref; if( ref $ref eq 'ARRAY' ) { my $t = 0; $src = sub{ $t < @{ $ref } ? $ref->[ $t++ ] : undef }; } my $_cmp = sub { local( $::a, $::b ) = @_; $cmp->() }; my @top = sort $cmp map $src->(), 1 .. $n; while( defined( $_ = $src->() ) ) { next if $_cmp->( $top[ $#top ], $_ ) <= 0; my $p = $#top; while( $p > 0 ) { $_cmp->( $top[ --$p ], $_ ) <= 0 and last; } splice @top, $p, 0, $_; pop @top; } return @top; } my @in = 1 .. 100; print for bestN sub{ $::b <=> $::a }, 10, \@in; open DICT, '<', $ARGV[0] or die $!; print for bestN sub{ length( $::b ) <=> length( $::a ) }, 10, sub{ local $^W; chomp( $_ = ); $_ };;