use strict; my @array = (0..999); # retrieve only the first 10 elements: my @top_ten = @array[0..9]; print "$_\n" for @top_ten; #### # ... original code for ((sort { $counted{$b} <=> $counted{$a} } keys %counted)[0..2]) { # ... etc. } #### # pick one: my $max = 2; # use [0..$max] my $max = 3; # use [1..$max] or [0..$max-1]