in reply to How can one find five max values and five min values with positions in descending and ascending order from arrays?

G'day supriyoch_2008,

You should take a look at sort. Is there something you don't understand about ascending and descending sorts? This technique worked for me:

$ perl -Mstrict -Mwarnings -E ' my @x = qw/c d e f k l m n/; my @y = qw/4 6 5 2 9 7 8 3/; my @sorted_y = sort { $a <=> $b } @y; my %yx_map = map { $y[$_] => [$x[$_], $_] } 0 .. $#y; say q{Max values:}; say "$_ = $yx_map{$_}[0] at $yx_map{$_}[1]" for reverse @sorted_y[ +-5..-1]; say q{Min values:}; say "$_ = $yx_map{$_}[0] at $yx_map{$_}[1]" for @sorted_y[0..4]; ' Max values: 9 = k at 4 8 = m at 6 7 = l at 5 6 = d at 1 5 = e at 2 Min values: 2 = f at 3 3 = n at 7 4 = c at 0 5 = e at 2 6 = d at 1

-- Ken

  • Comment on Re: How can one find five max values and five min values with positions in descending and ascending order from arrays?
  • Download Code