Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Size of an Hash reference

by dvinay (Acolyte)
on Nov 11, 2013 at 14:31 UTC ( #1061996=perlquestion: print w/replies, xml ) Need Help??
dvinay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

i have basic doubt regarding the hash references,

basically i wanted to check the emptyness of the hash

reference in my code,when there are key-value pairs i get

the correct count, but when there are no key-values

pairs, still my hash reference shows the below output

$VAR1 = \{ '' => undef };

why this undef value existing in hash reference, and

size of the hash reference returns as 1" how do i check the emptyness here.

any idea please...!

Replies are listed 'Best First'.
Re: Size of an Hash reference
by choroba (Chancellor) on Nov 11, 2013 at 14:46 UTC
    It depends on how you populate the hash. Compare:
    use Data::Dumper; $hashR = {}; print Dumper $hashR; $hashR->{$x} = $y; print Dumper $hashR


    $VAR1 = {}; $VAR1 = { '' => undef };
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Exactly i used the second way to populate my hash

      $hashR->{$x} = $y;

      reference, so is it because of this, that there is an empty key pointing to undef value

      anything wrong in populating the hash reference in above mentioned way...?

        Yes, but, apparently, neither $x nor $y was defined when you did it. The remedy is probably to check that $x and $y are defined before populating the hash with these values (especially $x).

Re: Size of an Hash reference
by hippo (Monsignor) on Nov 11, 2013 at 14:44 UTC
    use strict; use warnings; use Data::Dumper; my $hr = {}; print Dumper($hr);


    $VAR1 = {};

    So your hash ref above is not really empty whereas mine here is.

Re: Size of an Hash reference
by LanX (Chancellor) on Nov 11, 2013 at 14:53 UTC
    As a side note: your dump isn't showing a hash ref but the reference to a hash ref...

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Size of an Hash reference
by Anonymous Monk on Nov 11, 2013 at 14:44 UTC
    Show your code.   The hash is not, in fact, empty ... it contains a tag consisting of an empty-string pointing to a value that is undef. How it got to be that way is the root bug in your program. The only way that this could have happened is if your code did it, and you didn't know it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061996]
Approved by keszler
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2017-02-23 15:14 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (347 votes). Check out past polls.