It's a precedence problem. I've been trying to parenthesize this according
to the precedence chart in "Programming Perl" but haven't been having any
luck. Maybe someone elses brain is working better than mine and can tell us
how this is being seen by Perl with parens!
If you put parens around the final assignment it works:
$num == 3 ? $nextnum = 4 : ($nextnum="unknown");
Here is a better way to do it:
$nextnum = ( $num == 3 ? 4 : "unknown");
----- Original Message -----
From: "David Rankin" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, August 13, 2001 3:50 PM
Subject: Tertiary Operator Question
Hi Everybody,
I'm having trouble figuring out the way the tertiary operator evaluates
and returns data. Here's what I don't get . . .
If I do this:
#!/usr/bin/perl -w
use strict;
my $num=3;
$num==3 ? print "Num equals 3" : print "Num does not equal 3";
I get what I'd expect, "Num equals 3" gets printed.
But, if I do this:
#!/usr/bin/perl -w
use strict;
my $num=3;
my $nextnum;
$num==3 ? $nextnum=4 : $nextnum="unknown" ;
print $nextnum;
It prints "unknown". I'd expect it to print "4" because $num==3 would
evaluate to true.
I've looked in all my books and perldoc but everything seems to say the
same thing: If the operand before ? is true then operand before : is
evaluated and returned, otherwise, the operand after : is evaluated and
returned.
If anyone can clarify the results I'm getting, I'd really appreciate it.
Thanks!
-Dave
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]