Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help deciphering code (? :)

Status
Not open for further replies.

javalsu

Programmer
Jan 11, 2007
5
US
I'm a developer and I have to convert a perl program to vb.net. I'm pretty much done but there is one line of code that is giving me a headache, I can't find it anywhere.
Please help.

Here's the line.

$wnp += ( $p{$ng} ? log($p{$ng}) : $uns );

The variable p is a hash table %p, and I understand the log function.

The question is what the "?" and ":" mean. It reminds me a bit of fortran, but I still can't make it out, and I'm running through texts at work with no luck.

please help.
 
It's a ternary operator.

my $example = $exists ? "FOUND" : "NOT FOUND";

is the equivalent to

if ($exists) {
$example = "EXISTS";
}
else {
$example = "MISSING";
}

M. Brooks
 
See if this helps:
perlop said:
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 argument before the : is returned, otherwise the argument after the : is returned. For example:

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

Scalar or list context propagates downward into the 2nd or 3rd argument, 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;
So your line of code could be rewritten as:
Code:
if ($p{$ng}) {
    $wnp += log($p{$ng});
} else {
    $wnp += $uns;
}
 
It is the "Ternary Operator", which is used like an if-then-else. It is the same as used in C programs.

Used as:
expression ? if_true : if_false

I hope that helps.

Mike
 
MillerH

A. Pedant writes: After the hard time you gave KevinADC for his minor transgression on the other thread, I feel obliged to point out that
Code:
my $example = $exists ? "FOUND" : "NOT FOUND";

is not equivalent to

Code:
if ($exists) {
    $example = "EXISTS";
}
else {
    $example = "MISSING";
}
but
Code:
if ($exists) {
    $example = "[red]FOUND[/red]";
}
else {
    $example = "[red]NOT FOUND[/red]";
}

[wink]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
A.N. Other Pedant writes:

Actually, the scope of $example is different, it should be:
Code:
my $example;
if ($exists) {
    $example = "FOUND";
}
else {
    $example = "NOT FOUND";
}
. . . but now we're REALLY splitting hairs ;-)
 
Oooh. It wasn't MillerH who posted above, but MBrooks.

My bad. [blush]

The comments are still valid though, just the context.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
it's ternary operator.... [lookaround] [roll1]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top