in reply to exists() for a hash child-key creates the parent key? Bug?

When I have a “chain of keys” to work through, I sometimes use a loop ... something like this (untested) example:

my $found = 1; my $cursor = $hashref; for my $key ( qw/$key1 $key2 $key3/ ) { if (exists( $cursor->{$key} ) { $cursor = $cursor->{$key}; } else { $found = 0; last; // NOT FOUND - QUIT LOOKING NOW } } . . . if ($found) { // $cursor REFERS TO THE ELEMENT THAT WAS FOUND ... }

I hope that the approach is fairly self-explanatory.   $cursor advances through the data-structure (hashref $hashref), $key by key, as long as the keys being sought exist.   Anytime a key is found not to exist, the loop ends early and $found becomes False.   If the loop completes and $found is still True, the entire chain of keys exists and $cursor now points to whatever’s at the end of it all.

Replies are listed 'Best First'.
Re^2: exists() for a hash child-key creates the parent key? Bug?
by Anonymous Monk on Dec 18, 2014 at 12:13 UTC

    Even though there are a few nits I could pick with the (pseudo-)code, I'm not going to do that, because this is a step towards the kind of contribution I'd love to see more of. ++

      Even though there are a few nits I could pick with the (pseudo-)code, I'm not going to do that, because this is a step towards the kind of contribution I'd love to see more of. ++

      Not at all. Don't need sundialsvc4 to pull a Khen1950fx . Pure word bullpucky is easier to ignore than pretend code.