I have what I think is not a unique problem, just one that I cannot seem to successfully solve.

I have a hash of hashes, to N levels deep (usually around 8 to 10). Each hash has some normal hash items, and one is the one with the deep children, at each level. The deep one always has a numeral for a key, so /\d+/ works to find it.

Anyhow, I need a routine that will recurse through the hash, starting at the top, looking for a particular key number. If found, I need it to return THAT value.

I've been writing a recursive subroutine, but it seems to fail.

Here it is:
my ($newtop,$error) = _get_top($wanted_number,$tippy_top_hash); and sub _get_top { my $top_req = shift; my $top_item = shift; if ($top_item->{'ctl_id'} == $top_req) { return ($top_item,undef); } ## Added for speed if (exists($top_item->{$top_req})) { return($top_item->{$top_req},undef); } ## End added for speed foreach my $key (keys %{$top_item}) { next unless ($key =~ /^\d+/); if ($key == $top_req) { return ($top_item->{$key},undef); last; ### probably not needed } else { _get_top($top_req, $top_item->{$key}); } return (undef,"Seems we can't get the top item hash!"); } }
This is failing miserably, always returning undef,"Seems . . ."
Liberal prints to STDERR reveals that it does find the wanted hash item, but continues on below it!
If any of you geniouses out there can help, I would greatly appreciate it.

What does THIS little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

In reply to Recursing a hash by tame1

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.