Re: efficient max() function from sort
- From: jialinli1981@xxxxxxxxx (Jialin Li)
- Date: Wed, 28 May 2008 11:40:39 -0500
On Wed, May 28, 2008 at 9:17 AM, Ramprasad A Padmanabhan <ram@xxxxxxxxxxxxx>
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
PS: Posts via nntp to nntp.perl.org appear after a long time
Is there a more preferred way of posting here
Thanks
Ram
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
just compare one by one to find the max
my @z = qw(12 24 67 89 77 91 44 5 10);
my $max = $z[0];
map { $max = $_ if ($max < $_); } @z;
print $max;
or use List::Util which provides max subroutine
- References:
- efficient max() function from sort
- From: Ramprasad A Padmanabhan
- efficient max() function from sort
- Prev by Date: Need some help with XMLin
- Next by Date: Re: efficient max() function from sort
- Previous by thread: efficient max() function from sort
- Next by thread: Re: efficient max() function from sort
- Index(es):
Relevant Pages
|