Pathologically Eclectic Rubbish Lister PerlMonks

### "x ? y : z" notation

by apok (Acolyte)
 on Mar 26, 2009 at 00:19 UTC ( #753300=perlquestion: print w/ replies, xml ) Need Help??
apok has asked for the wisdom of the Perl Monks concerning the following question:

So, I've seen this sort of notation scattered around in various scripts and while I can see it is a form of logical check, the very nature of the characters makes it rather difficult to search for additional information on. (Alas, if only google used regexps.)

Would any of the more experienced monks kindly bestow upon me knowledge of the proper usage of this, or a link to where I can read up more? Or, at least, what the devil it is called?

Many thanks.

-----
-- Orson Welles

Comment on "x ? y : z" notation
Replies are listed 'Best First'.
Re: "x ? y : z" notation
by kyle (Abbot) on Mar 26, 2009 at 00:26 UTC

Conditional aka ternary operator in perlop.

Ahhh, thank you!

-----
-- Orson Welles
Re: "x ? y : z" notation
by Old_Gray_Bear (Bishop) on Mar 26, 2009 at 00:32 UTC
It's called the 'conditional' or 'ternary' operator.
```<condition> ? <do if condition was true> : <do if it wasn't>
An example:
```\$x == 1 ? print "x was '1'" : print "x was something else";

----
I Go Back to Sleep, Now.

OGB

\$x == 1 ? print "x was '1'" : print "x was something else";

While that's technically correct it doesn't take advantage of the fact that the ternary operator has a return value; whatever expression is evaluate after the ?.

```print( "x was " . \$x == 1 ? "'1'" : "someting else" );

Dave

take advantage of the fact that the ternary operator has a return value
How true; Perl is a bit unorthogonal in this respect, in that there is (at least) one case where you can achieve this effect with a plain 'if':

```sub lessthan {
if(\$_[0] < \$_[1]) {1} else {0}
}
Some languages (old examples: Algol60 and Algol68) are more regular in this respect...

BTW, does someone know by chance which was the first language which used ?: to represent "ternary if"? Of course you had this already in LISP or BCPL, but with a different syntax. The earliest occurance of ?: I know of, is in C, but I wouldn't be surprised if it was used in even older languages already.

--
Ronald Fischer <ynnor@mm.st>
Thanks for the explanation! I thought it might be something like that.

-----
-- Orson Welles
Re: "x ? y : z" notation
by Ish (Acolyte) on Mar 26, 2009 at 00:45 UTC
From the ActiveState documentation of perlop
```Conditional Operator

Ternary "?:" is the conditional operator, just as in C. It works much
+like an if-then-else. If the argument before the ? is true, the argum
+ent before the : is returned, otherwise the argument after the : is r
+eturned. For example:

printf "I have %d dog%s.\n", \$n,
(\$n == 1) ? '' : "s";

Scalar or list context propagates downward into the 2nd or 3rd argumen
+t, whichever is selected.

\$a = \$ok ? \$b : \$c;  # get a scalar
@a = \$ok ? @b : @c;  # get an array
\$a = \$ok ? @b : @c;  # oops, that's just a count!

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;

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)

That should probably be written more simply as:

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

I find it very useful in some of the 'log' wrappers I have written.
Re: "x ? y : z" notation
by ambrus (Abbot) on Mar 27, 2009 at 10:38 UTC
(Alas, if only google used regexps.)

Create A New User
Node Status?
node history
Node Type: perlquestion [id://753300]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2015-11-30 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?