Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

dumping a hash

by herby1620 (Monk)
on Aug 08, 2006 at 23:26 UTC ( [id://566281]=perlquestion: print w/replies, xml ) Need Help??

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

Am I an idiot, or what? I'm trying to dump a simple hash. I thought it would be easy, just call some nice routine that will output all the key/value pairs in the hash. So, I pick up my handy _Perl in a Nutshell_ book (quite nice actually) and they have a description of Data::Dumper. I look at "simple procedural interface" and enter:
%hash = {this => "one", that => two}; print Dumper (%hash);
Which gives a nice thing indicating that the argument is a hash (duh!).

I'm basically a "printf" kind of debugger. It works quite well, and I've used this in Perl. This SHOULD be a simple task without TOO much trouble. Someone must know a SIMPLE solution that will print out the key/value pairs kinda like:

this => "one", that => "two"

Of course I could write a routine (a bother!) that will iterate thru the keys, and print the values, but HASN'T someone done this already, or haven't I looked at the right place. Argggggg!!

The next lesson will be writing a "hello world" program :-)

Replies are listed 'Best First'.
Re: dumping a hash
by imp (Priest) on Aug 08, 2006 at 23:34 UTC
    You almost had it. Data::Dumper wants a reference to the hash, not the hash itself.
    %hash = (this => "one", that => two); print Dumper (\%hash);
      Also note that it should be
      %hash = (this => "one", that => two);
      Instead of
      %hash = {this => "one", that => two};
      You use {} to initialize a hash reference, and () to initialize an actual hash.
Re: dumping a hash
by GrandFather (Saint) on Aug 09, 2006 at 00:04 UTC

    Add use strict; use warnings; at the start of every script you write. They will save you time! In this case the two errors in your hash assignment would have been flaged. One at compile time (two as a bareword) and the other at run time (the reference as 'Reference found where even-sized list expected').

    and in the interests of TIMTOWTDI:

    use warnings; use strict; use Data::Dumper; use Data::Dump::Streamer; my %hash = (this => 'one', that => 'two'); print "Dumpered:\n" . Dumper (\%hash); print "\nStreamered:\n"; Dump (\%hash); print "\nikegamied:\n"; foreach my $key (keys %hash) { my $val = $hash{$key}; print("$key => $val\n"); } print "\nGrandFathered:\n"; print "$_ => $hash{$_}\n" for keys %hash; print "\nmapped:\n"; print map {"$_ => $hash{$_}\n"} keys %hash;

    Prints:

    Dumpered: $VAR1 = { 'that' => 'two', 'this' => 'one' }; Streamered: $HASH1 = { that => 'two', this => 'one' }; ikegamied: that => two this => one GrandFathered: that => two this => one mapped: that => two this => one

    DWIM is Perl's answer to Gödel
Re: dumping a hash
by planetscape (Chancellor) on Aug 09, 2006 at 01:04 UTC
Re: dumping a hash
by ikegami (Patriarch) on Aug 08, 2006 at 23:35 UTC
    You meant
    # Parens, not curlies my %hash = (this => "one", that => "two"); # Use a slash when dumping @var or %var print Dumper(\%hash); print($hash{this}, "\n");

    Curlies create an anonymous hash and return a reference to that hash. Hashes are initialized from a list, not from a hash reference. Use parens (not curlies) to create a list.

    If you wanted to work with a hash reference, use curlies, and assign the result of the curlies to a scalar.

    my $hash = { this => "one", that => "two" }; print Dumper($hash); print($hash->{this}, "\n");
Re: dumping a hash
by McDarren (Abbot) on Aug 09, 2006 at 04:05 UTC
    I quite like Data::Dumper::Simple, and generally use it in preference to Data::Dumper. It's much easier to use, and the output is more readable (especially with more complex data structures).

    For example, using your sample above:

    use Data::Dumper; my %hash = ( this => "one", that => "two", ); print Dumper(\%hash);
    prints..
    $VAR1 = { 'that' => 'two', 'this' => 'one' };
    But using Data::Dumper::Simple...
    use Data::Dumper::Simple; my %hash = ( this => "one", that => "two", ); print Dumper(%hash);
    prints...
    %hash = ( 'that' => 'two', 'this' => 'one' );

    Notice two things:

    • There is no need to pass a reference to the hash to Dumper.
    • The output includes the variable (hash) name, which makes it easier to read.

    One thing you should be aware of is that Data::Dumper::Simple uses source filtering techniques (which is probably why many people refuse to use it). Personally, this doesn't bother me as I only ever use it for debugging.

    Cheers,
    Darren :)

Re: dumping a hash
by gam3 (Curate) on Aug 09, 2006 at 02:05 UTC
    While I use Dumper most of the time myself, this one liner can also come in handy:
    %hash = (this => 'one', that => 'two'); print join(', ', map({ "$_ => $hash{$_}" } sort keys %hash)), "\n";
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found