Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Conditional Operator Confusion

by Melly (Hermit)
on Nov 29, 2006 at 15:48 UTC ( #586722=note: print w/ replies, xml ) Need Help??

in reply to Re: Conditional Operator Confusion
in thread Conditional Operator Confusion

Hey! The camel book told me it was just like an if-else (well, sort of). Hmm, I must admit that I'm still slightly in the dark here - none of the examples given so far help me understand exactly what is going on...

map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e** 2-$d**2+$b);$c=$d**2+$e**2>4?_:0}1..99;print$c}0..59;print$/}0..20;
Tom Melly,

Comment on Re^2: Conditional Operator Confusion
Download Code
Replies are listed 'Best First'.
Re^3: Conditional Operator Confusion
by Herkum (Parson) on Nov 30, 2006 at 16:44 UTC

    The trinary is much like any other operator, for example ==. Lets try this example,

    my $result = $a == $b

    Here, you get the result of the == operator, is $a equal $b. If they match it returns true, if they don't match it returns false.

    The trinary works in a similar manner except you are setting the value returned instead of the implied value of other operators.

    my $result = $a == $b ? 1 : 0;

    This does the same thing as the first example, except you are explicitly setting the value returned. Does this help?

      Not really... I'm sure I'm being really dumb here, but I still don't understand, in the following example, why $x gets set to "FALSE"... just very, very confused...

      1==1?$x='TRUE':$y='FALSE'; print "x=$x\n";
      Tom Melly,

        The conditional operator has higher precedence than the assignment operator, so
        is the same as

        The addition operator has higher precedence than the conditional operator, so
        is the same as

        If you want to do assignments, use if or add parens. Think of as the conditional operator as an operator that returns something.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2015-10-09 18:24 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (249 votes), past polls