use strict; use warnings; use Data::Dumper; my $nb_elements = shift; chomp $nb_elements ; my @masterArray; push @masterArray, ["", "", int rand (1e7), ""] for 1..$nb_elements; # print Dumper \@masterArray; my @top3 = sort {$b->[2] <=> $a->[2]} @masterArray[0..2]; my $min_top = $top3[2][2]; $nb_elements--; for my $sub_aref (@masterArray [3..$nb_elements]) { next if $sub_aref->[2] <= $min_top; @top3 = (sort {$b->[2] <=> $a->[2]} @top3, $sub_aref)[0..2]; $min_top = $top3[2][2]; } print Dumper \@top3;