Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Test Presence of Data in Hash

by bichonfrise74 (Vicar)
on Jul 20, 2009 at 20:36 UTC ( [id://781758]=perlquestion: print w/replies, xml ) Need Help??

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


I wanted to test if there are any data in a hash. So, I was thinking of simply using something like this...
#!/usr/bin/perl my %a = ( 'a' => '111' ); %a = (); print "test_1" if ( %a ); print "test_2" if ( defined( %a ) );
If I run this, 'test_1' is not printed which means there are no data in the hash. This is what I want.

But for some reason, 'test_2' is printed. Since I initialized the hash, shouldn't it be undefined already? I'm just curious why it printed. I checked the documentation and it did mention that using something like defined( %hash ) is already deprecated and I should just use ( %hash ) to test for the presence of the data in the hash.

Now, if I try to make a small change to this code where I declare and initialize the hash at the same time...
use strict; my %a = (); ## This is the change. print "test_1" if ( %a ); print "test_2" if ( defined( %a ) );
Then, nothing will be printed at all. Didn't I initialized the hash in both cases?

Any ideas on this?

Replies are listed 'Best First'.
Re: Test Presence of Data in Hash
by ikegami (Patriarch) on Jul 20, 2009 at 20:43 UTC
    keys(%hash) returns false if the hash is empty. This is what you want. [Upd: Actually just %hash in scalar context is better if you don't need a count. ]

    You'll never want to used defined on hashes and arrays. It reports on an unimportant internal property of those variables (whether or not some memory is allocated for data). This usage is even deprecated. Why aren't you using warnings?

    $ perl -wle'my %a; print defined(%a)?1:0' defined(%hash) is deprecated at -e line 1. (Maybe you should just omit the defined()?) 0
Re: Test Presence of Data in Hash
by davorg (Chancellor) on Jul 20, 2009 at 20:41 UTC

    This doesn't address the interesting questions you've raised about hashes. I'm sure that others will have good explanations of that, but I just wanted to point out that the best way to check if a hash contains any data is to use the keys function. In scalar context keys will return the number of keys defined in the hash.


    See the Copyright notice on my home node.

    Perl training courses

Re: Test Presence of Data in Hash
by bichonfrise74 (Vicar) on Jul 21, 2009 at 02:04 UTC
    I realized that defined( %hash ) is already deprecated and if I had 'use warnings', then I would have seen the deprecated message, but still it doesn't explain the output of the two codes.

    And doesn't 'deprecated' mean something like it should be avoided but still usable? If so, then the output of the above codes should have been the same.
Re: Test Presence of Data in Hash
by poolpi (Hermit) on Jul 21, 2009 at 12:08 UTC

    If you need to compare a hash against a (empty) hash ;)

    #!/usr/bin/perl use strict; use warnings; use Data::Compare; my %h1 = ( 1 => 'boo', 2 => 'bar', 3 => 'baz' ); my $c = new Data::Compare( \%h1, {} ); print '%h1 is ', $c->Cmp ? '' : 'not ', "empty.\n";


    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-15 15:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found