Re: efficient max() function from sort
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Wed, 28 May 2008 09:42:23 -0700
Ramprasad A Padmanabhan wrote:
I use sort to give the max of an array something like this
-----
my @z = qw(12 24 67 89 77 91 44 5 10);
my $max = ((reverse sort{$a <=> $b} (@z))[0]);
print "MAX = $max\n";
-----------
but when I am interested only in a single max value, I need not sort the entire array Is there a more efficient alternative to this
my $max = $z[ 0 ];
for ( @z ) {
$max = $_ if $max < $_;
}
print "MAX = $max\n";
Or:
use List::Util 'max';
my $max = max @z;
print "MAX = $max\n";
PS: Posts via nntp to nntp.perl.org appear after a long time
Is there a more preferred way of posting here
Have you subscribed to the mailing list?
http://lists.cpan.org/showlist.cgi?name=beginners
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.
- References:
- efficient max() function from sort
- From: Ramprasad A Padmanabhan
- efficient max() function from sort
- Prev by Date: Re: efficient max() function from sort
- Next by Date: error with repeated matching
- Previous by thread: Re: efficient max() function from sort
- Next by thread: Re: efficient max() function from sort
- Index(es):
Relevant Pages
|