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

comment on

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

a reccuring problem for me seems to be a way to acquire a reference to an arbritrary point in a data structure comprised of nested hashes.

suppose for example you have:

my %hash = ( 'company' => { 'microsoft' => {'applications' => {'windows' => 'operating system', 'office' => 'o +ffice suite'} ,'people' => {'bill gates' => 'chair man'} }, 'sun microsystems' => {'applications' => {'solaris' => 'operating system', 'star office' => 'office suite'} }, 'cross over' => {} } );
or something similar, now with this hash you could create a subroutine for each level of the data structure, say "sub add_company" or "sub add_application" but ideally what id like to be able to do is to seek to any level of the data structure and just throw in a value

to do that I suppose what you'd need to do is create a sub which takes a set of names to "crawl" along in the hash data structure, this would then find a reference to the part of the data structure you want, and then perform a set of operations to the 'location' referred to, independent of where that is.

with this you could then say something like:

add_to_hash(\%hash,'scalar','companies-microsoft-people-steve ballmer' +,'bills bulldog') #and you suddenly decide to have another category add_to_hash(\%hash,'list','companies-cross over-supports','iTunes','ph +oto shop') #or something more elegant
perhaps the above example could be perfomred using hash::merge, but still having an ability to find an arbitrary point in a data structure that can be then passed to a function would be immensely helpful, and if it seems like i'm trying to use hash structures like file hierachies, well yes, that's exactly right

the question i ask is, how to do it, I've tried using recursive hash references like

 \$hashref = $%hashref{foobar} 
and perl seems to really not like that, same with trying to construct the name of the hash from the root of the structure via non strict references

what am I missing?, any syntatic sugar, or a modules that I really should get to grips with


In reply to seeking in hash data structure by Maze

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 wandering the Monastery: (7)
As of 2024-04-23 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found