Do you know where your variables are? | |
PerlMonks |
Recursively search nested hash/array structuresby mp (Deacon) |
on Dec 09, 2004 at 19:16 UTC ( [id://413657]=perlquestion: print w/replies, xml ) | Need Help?? |
mp has asked for the wisdom of the Perl Monks concerning the following question:
I've done a fair amount of searching on google, perlmonks, and cpan for this but have not turned up much, except Data::DRef and Data::Walker. Data::Walker provides a file-system-ish interface command line interfce for manual examination of data structures, but does not provide methods for searching the structures. Data::DRef does not appear to have "search" methods to locate keys recursively, but I will continue look more closely at it.
I have a nested data structure (hash at the top level, then mixture of hashes and arrays at lower levels, arbitrarily deep) that is read from a config file (Config::General). I need a way to recursively search this data structure for particular hash keys, so that I can alter the data associated with them. For example, certain entries should always have a reference to an anonymous array, so I need to recursively search for a particular key, and wherever found, wrap it's value in an anonymous array if it is not already an array. So, for example, given: I need something that can find all occurrences of 'b' in this data structure, so that I can examine them and change them all to array references like this:
Similar functionality to what I need exists in File::Find, File::Find::Rule, HTML::TreeBuilder (especially it's look_down method), but these all target either filesystem or file traversal rather than perl data structure traversal. In the interest of avoiding reinventing a wheel that I'm sure exists somewhere, but I've been unable to find, has anyone found a CPAN module to do this sort of thing?
Back to
Seekers of Perl Wisdom
|
|