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

Hello all,

I am trying to use Data::Dumper to look at the data in a hash of hashes which looks something like:

$hash{$w1}{$w2} = { total_counts => $tot_counts, words => { $w3 => $count, } }

I have tried using:

print Dumper($hash);

and all I get on the command line is:

$VAR1 = undef;

I suspect this is pretty basic, but could someone please tell me what I am fogetting? Thanks!

-mox

Update

Problem solved. This one was more than simple, it was just stupid.

Mamas: Don't let your babies grow up to be the kind of programmers who leave their 'use strict' and 'use warnings' at home when they cut and paste subroutines into test scripts outside of their mother programs!

Thanks for the help all!

Replies are listed 'Best First'.
Re: Using Data::Dumper with a HoH
by davorg (Chancellor) on Oct 30, 2006 at 11:28 UTC

    You're putting your data into a hash called %hash. But you're trying to print out the contents of a scalar called $hash.

    use strict is your friend.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Good call.

      I was first thinking of another pitfall you have to watch out for, at least with strict off, and that is when while populating the hash, some entries may not be a ref or undef, but a string. In that case, Perl will go weird on you. For example:

      use Data::Dumper; no strict; %hash = ( a => 'x123' ); $hash{a}{b} = 'data'; print Dumper \%hash;
      which produces:
      $VAR1 = { 'a' => 'x123' };
      So, where did your data go to? It went into the global variable %x123, via a symbolic reference ${$hash{$a}}{b}.
      print Dumper \%hash, \%x123;
      produces
      Name "main::x123" used only once: possible typo at Z:\test.pl line 6. $VAR1 = { 'a' => 'x123' }; $VAR2 = { 'b' => 'data' };
      The "used only once" warning is a compile time warning, but using the symbolic reference (once) doesn't produce any warnings at all.

      *head strikes desk*

      Thanks for the hand. I will not wander from 'use strict' again! The printout is beautiful:)

      -mox
Re: Using Data::Dumper with a HoH
by liverpole (Monsignor) on Oct 30, 2006 at 11:45 UTC
    Hi chinamox,

    To follow up on what davorg is saying, make sure you pass %hash to Data::Dumper rather than $hash.

    You should get in the habit of passing things to Data::Dumper by reference, as well (ie. as \%hash):

    #!/usr/bin/perl -w + use strict; use warnings; use Data::Dumper; + my %hash; my ($w1, $w2, $w3) = ("blue", "marbles", "lottery"); my $count = 17; my $tot_counts = 42; + $hash{$w1}{$w2} = { total_counts => $tot_counts, words => { $w3 => $count } }; + # Use the reference of the hash (\%hash) rather than # just the hash values (%hash). # print Dumper(\%hash); __END__ == Output == $VAR1 = { 'blue' => { 'marbles' => { 'total_counts' => 42, 'words' => { 'lottery' => 17 } } } };

    And as davorg also alluded to, if you had put use strict at the beginning of your program, you could have caught the error already.

    Try running the following program, and then run it without "use strict" and "use warnings":

    #!/usr/bin/perl -w + use strict; use warnings; use Data::Dumper; + my %hash = ( 'test', 'program' ); print Dumper($hash); # Error: should be \%hash + __END__ == Output == Global symbol "$hash" requires explicit package name at hash_test line + 8.

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      Thank you for your advice and examples. I actually do use strict and use warnings normally. I had actually cut and pasted some code over and was working on figuring out how I was going to access the HoH. Unfortunately I didn't copy over the entire header like I should have.

      Also I will now use refrences with Data::Dumper from now on.

      Thank you,

      -mox

        It might behoove you to take the time to set up a skeleton Perl script and configure whatever editor you use to use that when you create a new Perl program (and if your editor doesn't support doing that, get a better editor).

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper qw( Dump ) ## Or . . . #use YAML::Syck qw( Load Dump ); ### ### CODE GOES IN HERE ### exit 0; __END__

        Many editors will make it possible that your cursor winds up with the "CODE GOES IN HERE" portion selected (so that it's replaced when you start typing or paste), or just after it.

Re: Using Data::Dumper with a HoH
by cog (Parson) on Oct 30, 2006 at 11:24 UTC
    What's on $w1, $w2 and $w3?

    Update: davorg's response below has the right answer :-)