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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional operator not working as expected. 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Can someone explain why I am not getting the expected result from my shorthand (if,then,else).

I have
Code:
if($rs[0]{'Dear'} ne ""){$body .= $rs[0]{'Dear'};
else{$body .= $rs[0]{'FirstName'};}

I expected the same result if i used
Code:
($rs[0]{'Dear'} ne "") ? $body .= $rs[0]{'Dear'} : $body .= $rs[0]{'FirstName'};
Only it doesn't matter whether the conditon evaluates to true or false, both get concatenated to the $body string?

Why is this happening?

Cheers,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Hi

1DMF said:
both get concatenated to the $body string?
Impossible. The ternary operator evaluates only one of the conditional arguments. Even more, your both codes works for me identically.

Note that your syntax is correct, but ternary usually is used as :
Code:
[navy]$body[/navy] [teal].=[/teal] [teal]([/teal][navy]$rs[/navy][teal][[/teal][purple]0[/purple][teal]][/teal][teal]{[/teal][green][i]'Dear'[/i][/green][teal]}[/teal] ne [green][i]""[/i][/green][teal])[/teal] [teal]?[/teal] [navy]$rs[/navy][teal][[/teal][purple]0[/purple][teal]][/teal][teal]{[/teal][green][i]'Dear'[/i][/green][teal]}[/teal] [teal]:[/teal] [navy]$rs[/navy][teal][[/teal][purple]0[/purple][teal]][/teal][teal]{[/teal][green][i]'FirstName'[/i][/green][teal]}[/teal][teal];[/teal]


Feherke.
 
It's not impossible I swear!

Wit the following data...

Code:
$rs[0}{'Dear'} = 'Bob';
$rs[0]{'FirstName'} = 'Robert';

i then had...
Code:
 my $body = "
        <html>
        <title>Compliance Documentation Declaration</title>
        <head>
        </head>
        <body>
        <p>Dear " . ($rs[0]{'Dear'} ne "") ? $rs[0]{'Dear'} : $rs[0]{'FirstName'} . ",</p></body></html>";

The output of the email body was "Dear BobRobert," ?

was it the way I've used the concatenation?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Hi

1DMF said:
was it the way I've used the concatenation?
Yes. Concatenation has higher priority than ternary.
Code:
[gray]# this[/gray]
[green][i]'foo'[/i][/green] [teal].[/teal] [purple]0[/purple] [teal]?[/teal] [green][i]'yes'[/i][/green] [teal]:[/teal] [green][i]'no'[/i][/green] [teal].[/teal] [green][i]'bar'[/i][/green]

[gray]# is the same as this[/gray]
[teal]([/teal] [green][i]'foo'[/i][/green] [teal].[/teal] [purple]0[/purple] [teal])[/teal] [teal]?[/teal] [green][i]'yes'[/i][/green] [teal]:[/teal] [teal]([/teal] [green][i]'no'[/i][/green] [teal].[/teal] [green][i]'bar'[/i][/green] [teal])[/teal]

[gray]# you probably want this[/gray]
[green][i]'foo'[/i][/green] [teal].[/teal] [teal]([/teal] [purple]0[/purple] [teal]?[/teal] [green][i]'yes'[/i][/green] [teal]:[/teal] [green][i]'no'[/i][/green] [teal])[/teal] [teal].[/teal] [green][i]'bar'[/i][/green]


Feherke.
 
Thanks Feherke,

I wondered what was going on there for a while!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top