Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Data Dump / Tool To View Hash Structure Easily?

by jdlev (Scribe)
on Sep 09, 2013 at 22:19 UTC ( #1053110=perlquestion: print w/ replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

It's driving me nuts trying to figure out how to access information from a hash nested within another hash. Is there some sort of quick and easy code that just will throw out the structure of a hash or multidimensional hash in an easy to read format? An example would be help because I can't figure this out. Thanks!

I love it when a program comes together - jdhannibal

Comment on Data Dump / Tool To View Hash Structure Easily?
Re: Data Dump / Tool To View Hash Structure Easily?
by tobyink (Abbot) on Sep 09, 2013 at 22:24 UTC

    Maybe Data::Dumper or Data::Dump. The latter is slightly more readable, but the former has the advantage of being bundled with Perl. (That is, you already have it installed!)

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Data Dump / Tool To View Hash Structure Easily?
by LanX (Canon) on Sep 09, 2013 at 22:24 UTC
    Well you already mentioned Data::Dump

    DB<143> use Data::Dump qw/dd/ DB<144> dd { A=>{ a=>1},B=>{b=>2}} { A => { a => 1 }, B => { b => 2 } }

    Which I prefer, but Data::Dumper is core.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Data Dump / Tool To View Hash Structure Easily?
by Laurent_R (Parson) on Sep 09, 2013 at 22:55 UTC

    Well, yes, of course, Data::Dumper and Data::Dump are very good solutions. But, it probably has to do with the way I am working on my programs and data structures, I am using much more often yet another solution: the x command of the Perl debugger. I am just using it all the time, be it for simply visualizing a simple array or for a complicated nested data structure.

Re: Data Dump / Tool To View Hash Structure Easily?
by rjt (Deacon) on Sep 09, 2013 at 23:46 UTC

    You mentioned having trouble "figuring out how to access information from a hash nested within another hash", and asked for an example. Once you learn this syntax, you'll be wanting to use it all over the place.

    For purposes of a concrete example, say you have a hash of users. Their usernames are keys, but the values are hash refs with additional information. It would look something like this (written here by hand, but not too different from a Data::Dump output):

    %users = ( rjt => { home => '/home/rjt', uid => 1000, shell => '/bin/bash', groups=> [ qw< sudo lpadmin > ], }, jdlev => { home => '/home/jdlev', uid => 1001, shell => '/bin/csh', groups=> [ qw< users lpadmin > ], }, );

    Note that the "groups" key refers to an array reference with the groups the user belongs to. Here are some examples of accessing the various data:

    print "rjt's uid is $users{rjt}{uid}\n"; local $" = ', '; # List separator print "jdlev is a member of: @{$users{jdlev}{groups}}.\n"; push @{$users{jdlev}{groups}}, 'sudo'; # jdlev now belongs to sudo as +well

    As for how to dump the data, Data::Dump gets my vote, most often anyway. I have some local debugging libraries that do various other data munging tasks to create more concise dumps in certain cases, but 99% of the time I could live with Data::Dump. It not being core doesn't concern me because none of the dump() calls survive to production code in my case.

    use strict; use warnings; omitted for brevity.
      Thanks for the great examples, they were very helpful. I have a question though. When you're using perl hashes, what is the difference between declaring a hash as "%hash" and "$hash"?

      I'm not sure why, but I've tried to print by using  $hash{113}{id} \n; and it won't put out anything for some reason? Meanwhile, Dumper works just fine and spews data all over the place?

      I love it when a program comes together - jdhannibal

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-12-21 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (101 votes), past polls