my @largest; while ( my($k,$v) = each %HASH ) { if ( @largest < $N ) { push @largest, $v; } else { # if $v is greater than the smallest element # in @largest, replace that element with $v } }