Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Data Dump / Tool To View Hash Structure Easily? by rjt
in thread Data Dump / Tool To View Hash Structure Easily? by jdlev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found