develooper Front page | perl.beginners | Postings from February 2002

Re: finding max value

Thread Previous | Thread Next
From:
Chas Owens
Date:
February 13, 2002 05:28
Subject:
Re: finding max value
Message ID:
1013606846.7503.11.camel@tert.icallinc.com
On Wed, 2002-02-13 at 08:08, Jon Molin wrote:
> Frank wrote:
> > 
> > On Wed, Feb 13, 2002 at 01:17:50PM +0100, Andrea wrote:
> > > In article <Pine.GSO.4.21.0202122117250.11490-100000@crusoe.crusoe.net> wrote "Jeff 'Japhy' Pinyan"
> > > <jeffp@crusoe.net>:
> > >
> > > >>I have a set of functions that give numeric results, and I need to compare  them and choose the
> > > >>maximal value. Is there any simple way of finding max?
> > >
> > > Don't think about it,
> > > just use the CPAN module List::Util.
> > >
> > > Then you only have to write
> > > my $max = max @values;
> > ---end quoted text---
> > 
> > or:
> > $max= (sort @values)[-1];
> 
> That doesn't seems like a good sollution,
> 
> @a = (-1, -5, -3);
> $max= (sort @a)[-1];
> 
> 
> gives -5 as max, it is max min but not max :)
> 

Perhaps is should be

$max = (sort { $a <=> $b } @a)[-1];

since sort defaults to using cmp (string compare).  

> 
> > 
> > Personally, I'd prefer Japhy's method for efficiency.
> 
> same here, it's straightforward
> 
> /jon
> > 
> > --
> >  Frank Booth - Consultant
> > Parasol Solutions Limited.
> > (www.parasolsolutions.com)
> > 
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
-- 
Today is Prickle-Prickle the 44th day of Chaos in the YOLD 3168
Fnord.

Missle Address: 33:48:3.521N  84:23:34.786W


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About