Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Hash of hash: testing for existence of higher level key (only)

by zuma53 (Beadle)
on Jun 22, 2012 at 04:18 UTC ( [id://977760]=perlquestion: print w/replies, xml ) Need Help??

zuma53 has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I have the following hash entries:
$hash->{'A'}->{'B5'}->{'C'} = 123; $hash->{'A'}->{'B5'}->{'D'} = 103; $hash->{'A'}->{'B2'}->{'C'} = 101;
I'd like to query the hash, but all I am given for inputs for queries are just values for the first two keys. Some examples: A/B5, A/B9, C/B2. If there's a hit (eg A/B5), then I could ask for the keys and iterate through that subhash.

My (limited) understanding of a hash is that for a 3-tier hash like this, you must always provide all 3 keys to get to test for existence.

I thought of
if (exists $hash->{'A'}->{'B2'}) ...
but I don't think that would work, as Perl would think that's another hash enitrely.
Or maybe something like
$hash->{'A'}->{'B2'}->{*}
Is there a way to do this without iterating through the levels of the HoH?

Thanks.

Replies are listed 'Best First'.
Re: Hash of hash: testing for existence of higher level key (only)
by AnomalousMonk (Archbishop) on Jun 22, 2012 at 05:19 UTC
    I thought of ... but I don't think that would work ...

    But what happened when you tried it?

    >perl -wMstrict -le "my $hash = { 'A' => { 'B5' => { 'C' => 123, 'D' => 103, }, 'B2' => { 'C' => 101, }, }, }; ;; if (exists $hash->{'A'}{'B2'}) { for my $k (keys %{ $hash->{'A'}{'B2'} }) { print qq{'$k' -> $hash->{'A'}{'B2'}{$k}}; } } " 'C' -> 101
      I tried that, but it never got to the loop. I double-checked and there was a hash key/value there.

      I will revisit this once again.

      Thanks for the help!
Re: Hash of hash: testing for existence of higher level key (diver)
by tye (Sage) on Jun 22, 2012 at 04:23 UTC
Re: Hash of hash: testing for existence of higher level key (only)
by NetWallah (Canon) on Jun 22, 2012 at 06:13 UTC
    If you know what the third level's key should be, you can check for existence:
    if (exists $hash->{A}->{B2}->{C}) { ...
    If you do not know, you will need to iterate:
    for my $k (keys %{ $hash->{A}->{B2} } ){ ...
    Why don't you TRY the code you think should work (or fail), and if it behaves differently from your expectations, we can help explain why.

    As for the thing you were attempting in your previous node:

    $hash->{'A'}->{'B2'}->{*} #is actually Equivalent to my @subkeys = keys %{ $hash->{A}->{B2} }; #at least - I think this is where you were heading...

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: Hash of hash: testing for existence of higher level key (only)
by frozenwithjoy (Priest) on Jun 22, 2012 at 05:40 UTC
Re: Hash of hash: testing for existence of higher level key (only)
by AnomalousMonk (Archbishop) on Jun 22, 2012 at 20:03 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977760]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found