Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Using ternary operator as lvalue in push

by ikegami (Patriarch)
on Jul 31, 2007 at 16:37 UTC ( [id://629864]=note: print w/replies, xml ) Need Help??


in reply to Re: Using ternary operator as lvalue in push
in thread Using ternary operator as lvalue in push

Youch, that's completely wrong.

First of all, the basic idea is wrong. Relational operators (like >) have higher precedence than the conditional operator (?:), so attempting to raise the precendence of > using parens is a no-go. (See perlop.) *

Furthermore, you actually made things worse by adding the parens because
push ($cond > 0) ? @a : @b, $elem;
is the same as
push($cond > 0) ? @a : @b, $elem;
is the same as
(push($cond > 0)) ? @a : @b, $elem;
which is quite wrong.

Finally, even if done right, the parens don't help. Both
push(($cond > 0) ? @a : @b, $elem);
and
push((($cond > 0) ? @a : @b), $elem);
produce the same result as the OP.

* — You might be thinking of the assignment operators rather than relational operators. Since the conditional operator (?:) has higher precedence than the assignment operators (like =), $cond ? $a = 1 : $b = 1; means ($cond ? $a = 1 : $b) = 1;.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://629864]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found