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

How do you get Dumper to print out a Hash of Hashes, I have tried:
print Dumper (\%hitsdb);
and this gives me:
$VAR1 = { 'nice' => 'HASH(0x1a9d730)' };
and print Dumper(\%hitsdb{&var}); gives me a compile error.

Replies are listed 'Best First'.
Re: Data::Dumper and Hash of Hashes
by davorg (Chancellor) on Aug 19, 2002 at 11:02 UTC
    We'd need to see the code where you create your hash to know what your problem is. This seems to work as I would expect:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %data = ( A => { one => 1, two => 2, three => 3 }, B => { four => 4, five => 5, six => 6 }, C => { seven => 7, eight => 8, nine => 9 } ); print Dumper(\%data);
    The output is:
    $VAR1 = { 'A' => { 'three' => 3, 'one' => 1, 'two' => 2 }, 'C' => { 'nine' => 9, 'eight' => 8, 'seven' => 7 }, 'B' => { 'six' => 6, 'five' => 5, 'four' => 4 } };
(jeffa) Re: Data::Dumper and Hash of Hashes
by jeffa (Bishop) on Aug 19, 2002 at 13:26 UTC
    This depends on how you assign your hash ref to the key nice - did you "stringify" a reference?
    my $nice = { foo => 'bar', baz => 'qux' }; my $mice = "three blind"; my %hitsdb = ( nice => "$nice", # this is the problem mice => "$mice", # this is a bad habit ); print Dumper \%hitsdb; __END__ $VAR1 = { 'nice' => 'HASH(0x80f8a20)', 'mice' => 'three blind' };
    Never put bare quotes around variables. If this is the case, then remove them and you should be fine.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Data::Dumper and Hash of Hashes
by Aristotle (Chancellor) on Aug 19, 2002 at 11:02 UTC
    Well, the latter needs to be print Dumper($hitsdb{$var}); but to be honest, I'm baffled by your actual problem since Data::Dumper is generally used because it will (usually) correctly print even deeply nested structures. I have yet to see a case where it didn't..
Re: Data::Dumper and Hash of Hashes
by ferrency (Deacon) on Aug 19, 2002 at 15:39 UTC
    When I've had to interpret these results, I've found that I did something like this:

    my %hash = {a => 1, b => 2, c => 3}; # curlies
    instead of this:
    my %hash = (a => 1, b => 2, c => 3); # parens
    The first assignment creates a hash with a single key which is a stringified hashref, with no value. The second assignment creates a hash with three keys and three values as expected.

    See if your code may be doing this as well.

    Alan