Some sample data would come in handy.

I would try a search function that takes a ref and uses ref to see if it is a hash, array or scalar. Then use that info to decide if it is what you're looking for, and if not, recurse on the contents. Eventually, return either undef, or a reference to the element in the structure you wanted and go from there.

For debugging, I'd suggest Dumpering the structure to a file, and building a string in the recursion to show where you are at the moment for printing useful comments.

It can also be handy to indent your prints to follow the recursion.

findthing($root, '$root->', ''); sub findthing { my $node = shift; my $debugString = shift; my $indent = shift; if (ref $node eq 'ARRAY') { for (0..$#$node) { my $result = findthing($node->[$_], $debugString . "\[$_\]", $in +dent .' '); return $result if defined $result; } print $indent . "No luck in array at $debugString\n"; }elsif (ref $node eq 'HASH') { for (keys %$node) { my $result = findthing($node->{$_}, $debugString . "\{$_\}", $in +dent . ' '); return $result if defined $result; } print $indent . "No luck in hash at $debugString\n"; } ... return undef; }

In reply to Re: Traversing arbitrarily deep and baggy structures by SuicideJunkie
in thread Traversing arbitrarily deep and baggy structures by anonymized user 468275

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.