Maze has asked for the wisdom of the Perl Monks concerning the following question:
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:
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 valuemy %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' => {} } );
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:
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 rightadd_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
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: seeking in hash data structure
by graff (Chancellor) on Oct 25, 2006 at 03:24 UTC | |
Re: seeking in hash data structure
by ikegami (Patriarch) on Oct 24, 2006 at 22:55 UTC | |
Re: seeking in hash data structure
by driver8 (Scribe) on Oct 25, 2006 at 01:33 UTC | |
by Maze (Sexton) on Oct 26, 2006 at 21:16 UTC | |
Re: seeking in hash data structure
by jbert (Priest) on Oct 25, 2006 at 08:50 UTC | |
by Corion (Patriarch) on Oct 25, 2006 at 08:58 UTC | |
by jbert (Priest) on Oct 25, 2006 at 10:05 UTC | |
Re: seeking in hash data structure
by ailivac (Sexton) on Oct 25, 2006 at 05:10 UTC | |
by Maze (Sexton) on Oct 26, 2006 at 21:55 UTC | |
Re: seeking in hash data structure
by gloryhack (Deacon) on Oct 25, 2006 at 03:27 UTC | |
Re: seeking in hash data structure
by bsb (Priest) on Jun 13, 2007 at 04:43 UTC |