Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

ternary operator

by kevind0718 (Scribe)
on Dec 16, 2008 at 23:13 UTC ( [id://730761]=perlquestion: print w/replies, xml ) Need Help??

kevind0718 has asked for the wisdom of the Perl Monks concerning the following question:

Hello kind and Wise Monks:

I have a bit of simple code and do not understand why it does not behave as I expect.

$col->[26] = (($col[26] eq '0') ? '' : $col->[26] ); print ' $col->[26]:' . $col->[26] . ":\n" ;
$col->26 is expected to contain a string which is a date. I.e. '20090711'
If it does not contain a date it contains a zero. I.e. 0
Not what I would like, but I have to deal with it.

If $col->26 contains '20090711' I get $col->26:20090711: from the above code.

If $col->26 contains '0' I get $col->26:0: from the above code.

Not what I expected. I want to get an empty string as in ''.

So the question is, what am I doing wrong?
Thanks for your assistance.

KD

Replies are listed 'Best First'.
Re: ternary operator
by ikegami (Patriarch) on Dec 16, 2008 at 23:18 UTC

    Use use strict;!
    Use use warnings;!
    Both would have caught the error independently.

    $col->[26] = (($col[26] eq '0') ? '' : $col->[26] );
    should be
    $col->[26] = (($col->[26] eq '0') ? '' : $col->[26] );
      yes that is much better.
      Been a long day.

      thanks

      kd
Re: ternary operator
by Tanktalus (Canon) on Dec 16, 2008 at 23:19 UTC

    Probably, you're not using strict and warnings. Either that, or you have both a scalar $col and an array @col in the same scope, which is just asking for confusion. Or, I suppose, both, but we'll assume just one at a time for now ;-)

    $col->[26] = $col->[26] eq '0" ? '' : $col->[26]; # note the extra -> +in there that was missing from your code. # or, more succinctly: $col->[26] = '' if $col->[26] eq '0'; # or, probably better yet: $col->[26] ||= '';
Re: ternary operator
by moritz (Cardinal) on Dec 16, 2008 at 23:20 UTC
    $col->[26] = (($col[26] eq '0') ? '' : $col->[26] ); ^^^

    You're missing an arrow here. Start all your scripts with

    use strict; use warnings;

    and declare your variables, that would have helped to catch this error.

Re: ternary operator
by graff (Chancellor) on Dec 17, 2008 at 01:58 UTC
    This logic (corrected as per previous replies):
    $col->[26] = (($col->[26] eq '0') ? '' : $col->[26] );
    could also be expressed as:
    $col->[26] ||= '';
    If the array element in question contains "0", that will evaluate to false, and the value will be set to the empty string; the assignment will also happen if the array element is undef (which might be a good thing to handle), and if it is already an empty string (which causes an insignificant redundancy).

    But if I were expecting a field to contain an 8-digit date, I'd be tempted to be at least a little more explicit about that:

    $col->[26] = '' unless ( $col->[26] =~ /^\d{8}$/ );
    That still allows dates like "00000000" (which would have been caught by the "||=" operator) or "99999999" (which would probably be easy to catch by using one of the Date::... modules to validate the value).

    Deciding how careful you want to be is a question of trade-offs -- striking the right balance between flagrant carelessness and compulsive precision -- and how well you know your input data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://730761]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-19 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found