in reply to Traversing arbitrarily deep and baggy structures

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; }

Replies are listed 'Best First'.
Re^2: Traversing arbitrarily deep and baggy structures
by anonymized user 468275 (Curate) on Feb 16, 2011 at 21:27 UTC
    I think much more than this suggestion is already outlined in the OP. As for sample data - the OP also explains how to get it - it really is too nasty to post!

    One world, one people