Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by kcott (Archbishop)
on Apr 26, 2013 at 05:59 UTC ( [id://1030768]=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?

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

Log In?
Username:
Password:

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

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

    No recent polls found