Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by Random_Walk (Prior)
on Apr 26, 2013 at 09:28 UTC ( [id://1030797]=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?

Keeping it simple. If you really only need the output you gave

#!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper; use constant IWANT => 5; my @names = qw/c d e f k l m n o p q r s t u/; my @vals = qw/4 6 5 2 9 7 8 3 3 5 8 8 2 4 12/; my @data; my $pos = 1; for my $val (@vals) { # Build a table my $name = shift @names; my $rec = sprintf "%2d corresponds to %s at pos %2d", $val, $name +, $pos++; push @data, $rec; } say "\nSmall to Big"; @data = sort @data; for (0..IWANT-1) { say $data[$_]; } say "\nBig to Small"; for (1..IWANT) { say $data[-$_]; }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!
  • Comment on Re: How can one find five max values and five min values with positions in descending and ascending order from arrays?
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-25 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found