Syntactic Confectionery Delight PerlMonks

### Re: Conditional Operator Confusion

by Herkum (Parson)
 on Nov 29, 2006 at 13:17 UTC ( #586687=note: print w/replies, xml ) Need Help??

in reply to Conditional Operator Confusion

```for (1..10) {
\$x = \$_ > 5 ? 'foo' : 'bar';
print "\$_:\$x\n";
}

Ah, I just noticed that you had tried that in your second example. The real issue that you had was that you were trying to treat a trinary operator like an if-else statement. I have tried that before and it just don't work! :)

Replies are listed 'Best First'.
Re^2: Conditional Operator Confusion
by Melly (Hermit) on Nov 29, 2006 at 15:48 UTC

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, pm@tomandlu.co.uk

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, pm@tomandlu.co.uk

Create A New User
Node Status?
node history
Node Type: note [id://586687]
help
Chatterbox?
 [Eily]: very useful default translation :P [Eily]: BTW LanX, you should try typing a few random chars at the beginning of each message. This will prevent expansion :P [LanX]: qwiud you sthink so? [LanX]: zxwqbd good idea! :) LanX embraces his new habit spqopiwjdnq [ambrus]: qQUkZTmHTuKxStGT- BzTIK9gdudif7TkTLI t3mnF144UaAZjkknXY 8nN-QM19wHBsTrp5vB lEYU_Kksa7X1RIBB4x EWLD5X7SW3jGX5ryfN OMn_yL5FTdQxzjhtyX mKN9sjUCzBNHK5Rrp0 S2WMUvIb1i9aZFgjtq VR0GH1bjPMvm1G16iz hBqc1U6toPd4FbJOFj VsOeT745AN1_pO88rD SRAYKtBZwCZedESZmN mvutrOTHiSNwflB- pRfn_k [Eily]: so far it seems to work Your Mother reminds the monks they should be grateful not to share an office, lest they be subjugated to constant inanities like, "Czech please!" [LanX]: what's strange is that the "Cowboy you said this already" message is missing #dqiwd [LanX]: YM: BTW learn to mute your humanity

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2017-03-27 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.