newbie questions re conditional operators 
Author Message
 newbie questions re conditional operators

Conditional Operators

Quote from perlop.pod:
"...It works much like an if-then-else.  If the argument before the ? is
true, the argument before the : is returned, otherwise the argument after
the : is returned..."

$ok = "ok";
$a = "ok";
$b = "match";
$c = "no match";

($a eq $ok) ? $a = $b : $a = $c;
print "$a\n";

I thought the above code means if $a stringwise equals $ok then make $a to
"match" else make it to "no match"

The above prints "no match", even through $a does eq $a.

Help appreciated!
Rory

--
please remove the 'x' to reply by mail



Sun, 02 Dec 2001 03:00:00 GMT  
 newbie questions re conditional operators

Quote:

> ($a eq $ok) ? $a = $b : $a = $c;

Complex precedence? Just: say no.

The ?: operator has higher precedence ("sticks together more tightly")
than assignment. So what you wrote is like this:

    (    ($a eq $ok) ? $a = $b : $a    ) = $c;

If the condition is true, it assigns $b to $a, then overwrites that
with $c.

You probably meant this:

    $a eq $ok ? ($a = $b) : ($a = $c) ;

Although this is better:

    $a = $a eq $ok ? $b : $c;

And this is better yet, in most cases:

    if ($a eq $ok) {
        $a = $b;
    } else {
        $a = $c;
    }

Quote:
> please remove the 'x' to reply by mail

please remove the 'x' to get replies by mail

Cheers!

--
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



Sun, 02 Dec 2001 03:00:00 GMT  
 newbie questions re conditional operators

Quote:

> Quote from perlop.pod:
> "...It works much like an if-then-else.  If the argument before the ? is
> true, the argument before the : is returned, otherwise the argument after
> the : is returned..."

If you're using a more recent version of Perl (i.e. 5.005_02 or later),
perlop.pod goes on to say:

    The operator may be assigned to if both the 2nd and 3rd arguments
    are legal lvalues (meaning that you can assign to them):

        ($a_or_b ? $a : $b) = $c;

    This is not necessarily guaranteed to contribute to the readability
    of your program.

    Because this operator produces an assignable result, using
    assignments without parentheses will get you in trouble.  For
    example, this:

        $a % 2 ? $a += 10 : $a += 2

    Really means this:

        (($a % 2) ? ($a += 10) : $a) += 2

    Rather than this:

        ($a % 2) ? ($a += 10) : ($a += 2)

--
 _ / '  _      /       - aka -

    /                                http://www.tiac.net/users/chipmunk/
        "It's funny 'cause it's true ... and vice versa."



Mon, 03 Dec 2001 03:00:00 GMT  
 newbie questions re conditional operators
Quote:

> Conditional Operators

> Quote from perlop.pod:
> "...It works much like an if-then-else.  If the argument before the ? is
> true, the argument before the : is returned, otherwise the argument after
> the : is returned..."

> $ok = "ok";
> $a = "ok";
> $b = "match";
> $c = "no match";

> ($a eq $ok) ? $a = $b : $a = $c;

  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

$a = ($a eq $ok) ? $b : $c;

Does that help you with your problem?

Quote:
> print "$a\n";

<SNIP>
--
Regards,

Chat Base: http://www.*-*-*.com/ | 250,000+ hits daily Worldwide!
TRG Software: http://www.*-*-*.com/ | CGI scripting in Perl/C, & more.
Unix/NT/Novell Administration, Security, Web Design, ASP, SQL, & more.
{*filter*} Programming & Consulting, Musician, Martial Arts, Sciences.


Mon, 03 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. A question on the Conditional Operator

2. Ternary Conditional Operator Question

3. conditional replacement (newbie question)

4. Conditional Operator behavior?

5. Three Operand Conditional Operator

6. push onto trinary conditional operator?

7. Q: conditional operator and variable assignment

8. ternary conditional operator

9. conditional operator "?:"

10. problem parsing conditional operator.

11. Perl cgi script - system operator - newbie question

12. Newbie question about or || operator

 

 
Powered by phpBB® Forum Software