Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2016-12-09 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (158 votes). Check out past polls.