if .. then .. else shorthand problem

Discussion in 'Perl Misc' started by Justin C, Jul 21, 2009.

  1. Justin C

    Justin C Guest

    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.

    --
    Justin C, by the sea.
     
    Justin C, Jul 21, 2009
    #1
    1. Advertising

  2. Justin C

    J. Gleixner Guest

    Justin C wrote:
    > 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. Gleixner, Jul 21, 2009
    #2
    1. Advertising

  3. Justin C

    Justin C Guest

    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.

    --
    Justin C, by the sea.
     
    Justin C, Jul 22, 2009
    #3
  4. Justin C

    Uri Guttman Guest

    >>>>> "JC" == Justin C <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Jul 22, 2009
    #4
  5. Justin C

    Ted Zlatanov Guest

    On Tue, 21 Jul 2009 14:21:26 -0000 Justin C <> wrote:

    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
     
    Ted Zlatanov, Jul 22, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. foreach shorthand

    , Mar 25, 2005, in forum: C++
    Replies:
    2
    Views:
    618
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Mar 25, 2005
  2. David W
    Replies:
    0
    Views:
    944
    David W
    Jan 17, 2007
  3. Foxpointe

    Shorthand for namespaces

    Foxpointe, Oct 31, 2006, in forum: XML
    Replies:
    4
    Views:
    448
    Foxpointe
    Oct 31, 2006
  4. Replies:
    0
    Views:
    312
  5. Replies:
    15
    Views:
    231
Loading...

Share This Page