if .. then .. else shorthand problem

J

Justin C

I've only just started using the shorthand for the "if .. then .. else"
statement: (test) ? this : or_that;

Here's what I have:

use strict;
use warnings;

my $weight = 40.18;

my $half_kilos = (( int($weight) -30 ) * 2);
print "$half_kilos\n";
if ( (($weight - 30) - (int($weight) - 30)) != 0 ) {
( ($weight - int($weight)) < 0.5 ) ? $half_kilos += 1 : $half_kilos += 2;
print "$half_kilos\n";
}

I expect $half_kilos to be 21. Failing that, 22. But I get 23!

There's probably something really simple here I've not spotted, can
someone please enlighten me? I admit that my example would be clearer
written as a regular 'if...then...else', but I'd only just remembered
these and had used one (in a simpler form), and wanted to drum it in, so
used it again... I can remember how they work now... but can't get this
one to work.

Thank you for any help you can give.


Justin.
 
J

J. Gleixner

Justin said:
I've only just started using the shorthand for the "if .. then .. else"
statement: (test) ? this : or_that;

Here's what I have:

use strict;
use warnings;

my $weight = 40.18;

my $half_kilos = (( int($weight) -30 ) * 2);
print "$half_kilos\n";
if ( (($weight - 30) - (int($weight) - 30)) != 0 ) {
( ($weight - int($weight)) < 0.5 ) ? $half_kilos += 1 : $half_kilos += 2;
print "$half_kilos\n";
}

I expect $half_kilos to be 21. Failing that, 22. But I get 23!

There's probably something really simple here I've not spotted, can
someone please enlighten me? I admit that my example would be clearer
written as a regular 'if...then...else', but I'd only just remembered
these and had used one (in a simpler form), and wanted to drum it in, so
used it again... I can remember how they work now... but can't get this
one to work.

Thank you for any help you can give.

Maybe this will show the issue more clearly.
my $weight = 40.18;

my $half_kilos = (( int($weight) -30 ) * 2);
print "$half_kilos\n";
if ( (($weight - 30) - (int($weight) - 30)) != 0 ) {
( ($weight - int($weight)) < 0.5 )
? $half_kilos .= 'a'
: $half_kilos .= 'b';
print "$half_kilos\n";
}
20
20ab


Solutions:

$half_kilos += ( ($weight - int($weight)) < 0.5 ) ? 1 : 2;

or

( ($weight - int($weight)) < 0.5 )
? ( $half_kilos += 1 )
: ( $half_kilos += 2 );
 
J

Justin C

OK, I see the error of my ways. The bottom line, I think, to get the
result you expect, is to always use parens for that type of statement.

Thanks to B&B, JG, and Tony for the help.


Justin.
 
U

Uri Guttman

JC> OK, I see the error of my ways. The bottom line, I think, to get the
JC> result you expect, is to always use parens for that type of statement.

nope. the bottom line is that you shouldn't do side effects with ?:. its
purpose is to return a value selected by a boolean test. the key word is
'return'. since perl allows side effects in expressions you are allowed
to put them inside ?: but it is very bad practice even if you use
parens. it is bypassing the purpose of the operator. if you want side
effects based upon a boolean, use if/else statements.

uri
 
T

Ted Zlatanov

JC> I've only just started using the shorthand for the "if .. then .. else"
JC> statement: (test) ? this : or_that;

JC> Here's what I have:

JC> use strict;
JC> use warnings;

JC> my $weight = 40.18;

JC> my $half_kilos = (( int($weight) -30 ) * 2);
JC> print "$half_kilos\n";
JC> if ( (($weight - 30) - (int($weight) - 30)) != 0 ) {
JC> ( ($weight - int($weight)) < 0.5 ) ? $half_kilos += 1 : $half_kilos += 2;
JC> print "$half_kilos\n";
JC> }

JC> I expect $half_kilos to be 21. Failing that, 22. But I get 23!

JC> There's probably something really simple here I've not spotted, can
JC> someone please enlighten me? I admit that my example would be clearer
JC> written as a regular 'if...then...else', but I'd only just remembered
JC> these and had used one (in a simpler form), and wanted to drum it in, so
JC> used it again... I can remember how they work now... but can't get this
JC> one to work.

I think you need to look at rounding and try to formulate the
calculation in general math terms, not if-then-else statements. What
you're doing above is like using a rock to open the window.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top