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.
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.