In the line
my $max = (sort {$b<=>$a} ($x, $y, $z))[0];

What is the "[0]" doing?

Also another question. While messing around with the compact version of if
statements I tried these.

$one = 1;
($one == 1) ? (print "\$one equals", print "$one") : (print "\$one does not
", print "equal 1");

Returns
1
$one equals 1

At first I thought the first "1" on a line by itself was a return flag or
something, until I did this

$one = 1;
($one == 1) ? ( print "$one", print "\$one equals") : (print "\$one does not
", print "equal 1");

Returns
$one equals 1
1

Then for a moment I just thought it was the order perl executes the
statements in the parens. But its printing "1" twice. "1" with the new line
by itself, then "1" at the end of the string where it is supposed to be...
But without a new line. If I change the conditional so that it is not true I
get some confusing results.

$one = 1;
($one == 2) ? ( print "$one", print "\$one equals") : (print "\$one does not
", print "equal 1");

Returns
Equal 1
$one does not 1

Reversed the order of the statements and this happens.

$one = 1;
($one == 2) ? ( print "$one", print "\$one equals") : (print "equal 1",
print "\$one does not ");

Returns
$one does not equal 1
1

I can live with reversing the order of statements when I want to do multiple
things in a short compact if. But why is it returning 1? Can it be
suppressed?

Ken

-----Original Message-----
From: Jenda Krynicky [mailto:[EMAIL PROTECTED]] 
Sent: Friday, January 10, 2003 10:04 AM
To: [EMAIL PROTECTED]
Subject: Re: Best way to return largest of 3 Vars?


From: Tim Musson <[EMAIL PROTECTED]>
> Hey Jenda,         
> 
> My MUA believes you used Pegasus Mail for Windows (v4.02a)
> to write the following on Friday, January 10, 2003 at 10:04:23 AM.
> 
> Thanks all! I know I can always count on this list!

:-)
 
> Looks like we are going with this one.
> 
> JK> sub Bob {
> JK>         my $max = (sort {$b<=>$a} ($x, $y, $z))[0];
> JK>         return $max;
> JK> }

According to the benchmarks this one was slightly better 

        my ($max) = sort {$b<=>$a} ($x, $y, $z);

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to