Let me see if I understand you correctly: after you've loaded values into your HoH, you're going to be looking at some new set of inputs "col1, col2, col3, ..." values, and you want to check for matches in the HoH.

Now, based on the HoH being keyed as "$hash{ $c1 }{ "$c2 _ $c3" } (where $c2 is always less-than-or-equal-to $c3), some new set of column values could give any of the following outcomes:

- (a) $hash{$col1} doesn't exist, or - $hash{$col1} exists, and... - (b) "$col2 _ $col3" gives an exact match to an existing key, or - (c) both $col2 and $col3 fall within the range of an existing key, + or - (d) only $col2 or $col3 (not both) fall within the range of an exi +sting key, or - (e) $col2 and $col3 define a range between existing keys, or - (f) $col2 and $col3 define a range that encompasses one or more ex +isting keys, with extra margins at one or both edges.
The particular solution you want will depend on what is supposed to happen for cases (d - f). It might also depend on whether or not the initial loading of the HoH is supposed to yield non-overlapping ranges. (If the "$c2-$c3" ranges for a given $c1 are not supposed to overlap, you might need to add sanity checks the input data, and/or conditions on the HoH loading, to make sure you satisfy that constraint.)

But all-in-all, I think it might be better to treat this as a database problem rather than a hash problem, because SQL already gives you the idioms you need to look for matches according to your criteria. You create a table and load it from your initial input, then for subsequent rows of data, you do a query like:

select * from table where col1 = ? and ( col2 >= ? and col3 <= ? )
(using placeholders for the column values -- see DBI for details)

Depending on what needs to be done about cases of partial or total lack of overlap (d - f above), you can add conditions to that query, and/or use additional queries.

(There might be ways to emulate that sort of SQL facility with hash keys, but it won't be as simple as SQL, I think.)


In reply to Re: Search in Hash of Hash for the range of values by graff
in thread Search in Hash of Hash for the range of values by snape

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.