Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by hdb (Monsignor)
on Apr 26, 2013 at 05:55 UTC ( [id://1030767]=note: print w/replies, xml ) Need Help??


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

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...

Replies are listed 'Best First'.
Re^2: How can one find five max values and five min values with positions in descending and ascending order from arrays?
by supriyoch_2008 (Monk) on Apr 27, 2013 at 05:39 UTC

    Hi hdb,

    Thanks for the code. It has solved my problem. Sorry for late reply.

    With deep regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030767]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found