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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |