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:

{ a => { b => 3, } b => 123, c => { b => [ 123, 456, 789, ], }, d => [ b => 456, ], }
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:
{ a => { b => [ 3, ] } b => [ 123, ], c => { b => [ 123, 456, 789, ], }, d => [ b => [ 456, ], ], }

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?


In reply to Recursively search nested hash/array structures by mp

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.