Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

hash undef?

by Anonymous Monk
on Nov 15, 2007 at 12:56 UTC ( [id://650964]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys, hope you can help on this one. I have a hash which is filled from lines in a file, after a split of the line a hash is filled defined as  $time{$recordNumber}. Later on in the script I want to check if  $time{$recordNumber} exists, and if not enter the if statement. I tried using
if ($time{$recordNumber} == undef) #147 { $time{$recordNumber} = 10000; }
but got  Use of uninitialized value at ././monitor-dataset-age line 147 It's not no, because it's not there...but I'm testing to see if it's not there!? Any help much appreciated, Many thanks, Kim

Replies are listed 'Best First'.
Re: hash undef?
by moritz (Cardinal) on Nov 15, 2007 at 12:59 UTC
    if (not defined $time{$recordNumber}){ ... }

    You can also check with exists in hashes, the semantic is a little bit different.

    Update: A bit explanation: == compares numbers, so in the expression $foo == undef the undef is promoted to 0 and compared with $foo - not what you want.


      perl -wMstrict -e "my %hash = (foo => 'x', bar => '', baz => undef); for (qw/foo bar baz quux/) { printf qq($_ exists:%s: $_ defined:%s: $_ true:%s: \n), exists($hash{$_}), defined($hash{$_}), $hash{$_} ? 1 : '' }" foo exists:1: foo defined:1: foo true:1: bar exists:1: bar defined:1: bar true:: baz exists:1: baz defined:: baz true:: quux exists:: quux defined:: quux true::
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: hash undef?
by zer (Deacon) on Nov 15, 2007 at 14:23 UTC
     if (exists $time{$recordNumber}) This will tell you if it actually exists rather than whether it holds an undef or not. Defined will return false if the value contains an undef or if it just doesnt exist.

    Update: Thank you Mr. Anony. I had said that Defined returns undef, which isnt accurate and even contradicts the use of the function.

      defined returns a boolean false (an empty string), not undef, if the value it is testing is not defined:

      perl -wMstrict -e "print 'defined returns defined' if defined defined undef" defined returns defined
Re: hash undef?
by dsheroh (Monsignor) on Nov 15, 2007 at 19:12 UTC
    As moritz has said, testing not defined is better than == undef, but $time{$recordNumber} isn't the only variable involved on that line... I suspect that $recordNumber is the uninitialized value that it's complaining about.

    (If $recordNumber is defined and $time{$recordNumber} isn't, I would expect to see the warning "Use of uninitialized value in numeric eq (==) at...".)

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-22 02:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found