http://www.perlmonks.org?node_id=1053110

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?

Replies are listed 'Best First'.
Re: Data Dump / Tool To View Hash Structure Easily?
by tobyink (Canon) 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 (Saint) 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 rjt (Curate) 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
Re: Data Dump / Tool To View Hash Structure Easily?
by Laurent_R (Canon) 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.