First of all, the question is whether x or y can be assumed to have unique values. Either of the two can then be used as keys to a hash to store the association of the two arrays. In this case, the task is straightforward.

In the general case, one needs to introduce the position as the key to a hash that stores x and y. One can then sort the keys with respect to y. Here is how it could look like:

use strict; use warnings; # initial data my @x = qw/c d e f k l m n/; my @y = qw/4 6 5 2 9 7 8 3/; # store in hash to keep association between x and y after sorting my %data; for my $i (0..@x-1) { $data{$i}{x} = $x[$i]; # store x corresponding to position $i $data{$i}{y} = $y[$i]; # store y corresponding to position $i } # sort positions with respect to y my @sorted = sort { $data{$a}{y} <=> $data{$b}{y} } keys %data; print "Sorted data:\n"; for my $i (@sorted) { print "$data{$i}{y} corresponds to $data{$i}{x} at position $i +\n"; }

To print the highest and lowest 5 should now be simple...


In reply to Re: How can one find five max values and five min values with positions in descending and ascending order from arrays? by hdb
in thread How can one find five max values and five min values with positions in descending and ascending order from arrays? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.