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

Is a hash of hashes all contained within the same memory space, or are the embedded hashes actually separate variables?
Example:
$hash{'key'} = '123'; #hash
$hash{'key'}{'subkey'} = '345'} #hash of hashes.
Is %hash{'key'} technically the same variable as %hash or is %hash{'key'} now a separate variable.

Replies are listed 'Best First'.
Re: hash of hashes and memory space...
by ikegami (Patriarch) on Mar 23, 2010 at 18:20 UTC

    Well, what's a variable to you?

    • Is each name a variable?

      Well, there's only one name.

    • Is each block of allocated memory a variable?

      $x has three given my $x = '123'; (SV head, SV body, string buffer).

      %hash has 9 (-ish) given my %hash; $hash{'key'} = '123';.

      my %hash; $hash{key}{subkey} = '123'; would have double minus two what my %hash; $hash{key} = '123'; has.

      (Not counting any memory used by the pad or by the memory allocation system.)

    • Or are you constraining yourself to SVs and their derivatives (AV, HV, etc)?

      $hash{key} = '123'; involves a hash (with a key and a number buckets) and a scalar.

      $hash{key}{subkey} = '123'; involves two hashes (with a key and a number buckets each) and two scalars (one a reference, the other a string).

    Your question doesn't make that much sense. What's your real question?

      My question came about from looking at the output of a Data::Dumper and Data::Dump of a hash of hashes I am working with. Dumping the hash only prints the first hash key/value pairs not the under lining hashes or their key/value pairs. This made me think that a hash of hashes is not one single variable (container) but that each embedded hash is it's own separate container.
        A "hash of hash" is really a "hash of references to hashes".
        $h{foo}{bar} = 123;
        is short for
        $h{foo}->{bar} = 123;
        and
        ${ $h{foo} }{bar} = 123;

        Something to try:

        my %a; my %b; $a{foo} = \%b; $a{foo}{bar} = 123; print("$b{bar}\n");

        Strange. When I use Data::Dumper, I get all the subhashes shown too. Example:

        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %hash= ( 4, {3,4,5,6,7,8}); $hash{a}{b}{c}{d}{e}=366; print Dumper(\%hash); #output: $VAR1 = { '4' => { '3' => 4, '7' => 8, '5' => 6 }, 'a' => { 'b' => { 'c' => { 'd' => { 'e' => 366 } } } } };

        Maybe $Data::Dumper::Maxdepth is set to 1 in your script

Re: hash of hashes and memory space...
by jethro (Monsignor) on Mar 23, 2010 at 18:40 UTC

    They are separate variables/constructs. The entry in the hash for key is just a reference aka pointer to the subhash. For example:

    my %hash; $hash{key}{subkey}=123; my $f= $hash{key}; $hash{key}= undef; #moving the subhash print $f->{subkey}; #would print 123

    Now the code above shows really nothing as this could all be done by internal magic, but in truth internally they are separate too.

    BUT... internally nearly everything is separate, even a variable name and its value. Or the hash (which is a big table of "buckets") and the hash values are in separate memory locations, in the bucket is only a pointer to the actual value.

      in the bucket is only a pointer to the actual value.

      Not even. The bucket is a pointer to a linked list of nodes, one of which in turn points to a key structure and to the value.

      illguts