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...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|