Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Finding the max()/min()

by itub (Priest)
on Nov 11, 2004 at 04:32 UTC ( #406918=note: print w/ replies, xml ) Need Help??


in reply to Re: Finding the max()/min()
in thread Finding the max()/min()

Perhaps this will count as "cheating" for the purpose of this thread, but I just do this to find the min/max of a list

use List::Util qw(min max); $min = min @list; $max = max @list;
;-)


Comment on Re^2: Finding the max()/min()
Download Code
Re^3: Finding the max()/min() (reduce)
by demerphq (Chancellor) on Nov 11, 2004 at 10:56 UTC

    A moderate annoyance of List::Util::m(in|ax) is that they dont operate on tied values or situations where there is some form of indirection involved (like finding the key whose value in a hash is the lowest). Then List::Util::reduce() is your friend:

    $min = reduce { ($a,$b)[$a < $b] } @list; $min_key = reduce { ($a,$b)[$x{$a} < $x{$b}] } keys %x;

    Working out max is left as an exercise :-)

    ---
    demerphq

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-07 12:41 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 (88 votes), past polls