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.

In reply to Hash of hash: testing for existence of higher level key (only) by zuma53

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.