Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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...!

Comment on Size of an Hash reference
Download Code
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.
Re: Size of an Hash reference
by hippo (Curate) on Nov 11, 2013 at 14:44 UTC
    use strict; use warnings; use Data::Dumper; my $hr = {}; print Dumper($hr);

    gives

    $VAR1 = {};

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

Re: Size of an Hash reference
by choroba (Abbot) 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

    Output:

    $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 LanX (Canon) 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)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-09-16 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (157 votes), past polls