in reply to Using Data::Dumper with a HoH
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using Data::Dumper with a HoH
by chinamox (Scribe) on Oct 30, 2006 at 12:02 UTC | |
by Fletch (Bishop) on Oct 30, 2006 at 14:31 UTC |