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

Ternary operator for only true or false? 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I was wondering what the correct syntax would be if i only wanted a returned value for true or false using the Ternary operator.

IE. if true give value otherwise don't or if false give value otherwise don't

I currently have
Code:
my $prod_type = $case->{'LoanType'} . ($case->{'LoanTypeSecondary'} == -1) ? '' : $case->{'LoanTypeSecondary'};

can you omit the 'else' :)) colon part so just have...

Code:
my $prod_type = $case->{'LoanType'} . ($case->{'LoanTypeSecondary'} != -1) ? $case->{'LoanTypeSecondary'};

It would be good and better than using
Code:
my $prod_type = $case->{'LoanType'};
if ($case->{'LoanTypeSecondary'} != -1){
    $prod_type .= $case->{'LoanTypeSecondary'};
}

Or would this be better...
Code:
my $prod_type = $case->{'LoanType'};
$prod_type .= $case->{'LoanTypeSecondary'} if $case->{'LoanTypeSecondary'} != -1;

Your advice is appreciated.

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
 
Any idea why when LoanType & LoanTypeSecondary are both = 0 , $prod_type = '0' instead of '00' ?

I've tried
Code:
my $prod_type = "$case->{'LoanType'}" . ($case->{'LoanTypeSecondary'} != -1) ? $case->{'LoanTypeSecondary'};

to try to force string concatenation but it isn't working.

Why?

"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
 
worked it out
Code:
my $prod_type = $case->{'LoanType'} .[COLOR=red yellow]=[/color] ($case->{'LoanTypeSecondary'} != -1) ? $case->{'LoanTypeSecondary'};

"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
 
'00': this was because [tt]!=[/tt] is a numeric operator and perl transforms the '00' into the number 0 before doing the compare.
But why compare a string with a number?
Concerning the original question, I prefer the [tt]do this if|unless ...;[/tt] construct. Clearer and probably more efficient.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
As far as the listed options go - I agree with Franko, I like the do this if ...[/b] notation as well.

And if you're not using the true part and false part, there's no reason to use the ternary operator.
 
Thanks guys, I'll probably change it to 'do this if', though the blank sting method is working.

Franco -> I'm not comparing a string, don't worry about that, they are numbers but i need them as strings else zero+zero is zero and I need '00'.

on a side note, why does '.=' work but not '.' , usually you can simply concatenate with '.'

when the '.' is used with numbers does it apply it as a mathematical operator rather than just 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
 
If you do this
Code:
my $prod_type = $case->{'LoanType'} .(($case->{'LoanTypeSecondary'} != -1) ? $case->{'LoanTypeSecondary'}:0);
you also get '00'.
It's a problem of operator precedence: probably your code is not behaving as you expect it to. Better check what happens with other values or make it safe with parentheses.
Note also that my perl (v.5.12.3) does not accept the ternary op with a non existent or void second option.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
? I certainly don't want to append a zero as you have shown.

'' isn't void is it? '' certainly isn't considered null.

Thanks for the precedence heads up!

Anyway I have tried the following but get an error,
Code:
    my $prod_type = $self->{CASE}{'LoanType'} .= ($self->{CASE}{'LoanTypeSecondary'} if $self->{CASE}{'LoanTypeSecondary'} != -1);

I was kinda hoping to have a one liner if possible, but if not possible two will have to do!

"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