Consider the following HoH:
$h = {
a => {
1 => x
},
b => {
2 => y
},
...
}
Is there a way to check whether a hash key exists on the second nested level without calling keys(%$h)
? For example, I want to say something like:
if ( exists($h->{*}->{1}) ) { ...
(I realize you can't use *
as a hash key wildcard, but you get the idea...)
I'm trying to avoid using keys()
because it will reset the hash iterator and I am iterating over $h
in a loop using:
while ( (my ($key, $value) = each %$h) ) {
...
}
The closest language construct I could find is the smart match operator (~~
) mentioned here (and no mention in the perlref perldoc), but even if ~~
was available in the version of Perl I'm constrained to using (5.8.4), from what I can tell it wouldn't work in this case.
If it can't be done I suppose I'll copy the keys into an array or hash before entering my while
loop (which is how I started), but I was hoping to avoid the overhead.