Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How can one call the lowest value of an array by reference?

by Athanasius (Monsignor)
on Dec 12, 2012 at 08:05 UTC ( #1008468=note: print w/ replies, xml ) Need Help??


in reply to How can one call the lowest value of an array by reference?

Here is one way:

#! perl use Modern::Perl; use List::Util qw( min ); my @vals = qw( 5 3 2 12 2 ); my @names = qw( Cat Bat Cow Dog Rat ); my $min_val = min @vals; my @indices = grep { $vals[$_] == $min_val } (0 .. $#vals); for (@indices) { printf "Lowest value: %d, index: %d, corresponding name: %s\n", $min_val, $_, $names[$_]; }

Output:

18:01 >perl 429_SoPW.pl Lowest value: 2, index: 2, corresponding name: Cow Lowest value: 2, index: 4, corresponding name: Rat 18:03 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: How can one call the lowest value of an array by reference?
Select or Download Code
Re^2: How can one call the lowest value of an array by reference?
by supriyoch_2008 (Scribe) on Dec 12, 2012 at 10:24 UTC

    Hi Athanasius,

    Thanks for your quick reply. Your code has worked nicely. It has solved my problem.

    Regards,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2014-07-25 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (174 votes), past polls