Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by davido (Archbishop)
on Dec 12, 2012 at 08:16 UTC ( #1008469=note: print w/ replies, xml ) Need Help??


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

This could probably be refactored more elegantly, but it seems to do the job in a single pass without undue complexity.

use strict; use warnings; my @values = qw( 5 3 2 12 2 ); my @names = qw( Cat Bat Cow Dog Rat ); my $min; my @indices; foreach my $ix ( 0 .. $#values ) { if( ! defined $min or $values[$ix] < $min ) { @indices = ($ix); $min = $values[$ix]; } elsif( $min == $values[$ix] ) { push @indices, $ix; } } print "Lowest value: $min\n"; print "$_ => $names[$_]\n" for @indices;

Dave


Comment on Re: How can one call the lowest value of an array by reference?
Download Code
Replies are listed 'Best First'.
Re^2: How can one call the lowest value of an array by reference?
by supriyoch_2008 (Scribe) on Dec 12, 2012 at 10:20 UTC

    Hi Dave,

    Thanks for your prompt reply. Your code is simple for me to understand. It has solved my problem.

    With regards,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2015-07-30 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls