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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |