Re: efficient max() function from sort

2008-05-28 Thread John W. Krahn
Chas. Owens wrote: On Wed, May 28, 2008 at 5:07 PM, Rob Dixon <[EMAIL PROTECTED]> wrote: snip $max = (sort {$a <=> $b} @z)[-1]; snip my $max = (sort { $b <=> $a } @z)[0]; is slightly faster than using [-1]. Or: my ( $max ) = sort { $b <=> $a } @z; John -- Perl isn't a toolbox, but a sm

Re: efficient max() function from sort

2008-05-28 Thread Rob Dixon
Chas. Owens wrote: > > On Wed, May 28, 2008 at 5:07 PM, Rob Dixon <[EMAIL PROTECTED]> wrote: > snip >> $max = (sort {$a <=> $b} @z)[-1]; > snip > > my $max = (sort { $b <=> $a } @z)[0]; > > is slightly faster than using [-1]. > > You should only use this form if performance matters and you know

Re: efficient max() function from sort

2008-05-28 Thread Chas. Owens
On Wed, May 28, 2008 at 5:07 PM, Rob Dixon <[EMAIL PROTECTED]> wrote: snip > $max = (sort {$a <=> $b} @z)[-1]; snip my $max = (sort { $b <=> $a } @z)[0]; is slightly faster than using [-1]. You should only use this form if performance matters and you know that @z is smaller than 250 items (over

Re: efficient max() function from sort

2008-05-28 Thread Rob Dixon
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

Re: efficient max() function from sort

2008-05-28 Thread John W. Krahn
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 enti

Re: efficient max() function from sort

2008-05-28 Thread Li, Jialin
On Wed, May 28, 2008 at 9:17 AM, Ramprasad A Padmanabhan <[EMAIL PROTECTED]> 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

efficient max() function from sort

2008-05-28 Thread Ramprasad A Padmanabhan
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 effici