in reply to Search in Hash of Hash for the range of values
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:
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.)- (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.
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:
(using placeholders for the column values -- see DBI for details)select * from table where col1 = ? and ( col2 >= ? and col3 <= ? )
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.)
|
|---|