Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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
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,

Reaped: Re^2: How can one call the lowest value of an array by reference?
by NodeReaper (Curate) on Dec 12, 2012 at 10:22 UTC

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 chanting in the Monastery: (9)
As of 2014-11-28 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls