in reply to Traversing a nested hash
# $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?
|
|---|