my @top = @masterArray[0..2]; @top = (sort { $b->[2] <=> $a->[2] } @top, $masterArray[$_])[0..2] for 3..$#masterArray;