So, you're looking for code that doesn't look like:
# $hash_ref is passed in and defined outside
# $key_to_find is what we want to find.
# We will return the value of the first $key_to_find
sub traverse_funky_hash {
my ($hash_ref, $key_to_find) = @_;
HASH_LOOP:
foreach (sort keys %$hash_ref) {
if ($_ eq $key_to_find) {
return ($hash_ref->{$_}, 1);
}
next HASH_LOOP unless ref($hash_ref->{$_} eq 'HASH');
my ($val, $rc) = traverse_funky_hash($hash_ref->{$_}, $key_to_
+find);
return ($val, $rc) if $rc;
}
return (undef, 0);
}
Obviously, that's for an arbitrarily-nested hash of hashes. (And, yes, I know I'm using ref. Sue me. *grins*)
Somehow, you want to make it so that you don't have to use the foreach over keys, right?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.