in reply to Date Ranges Question

If the data is not currently in a database and it is not possible to put it in one, then you should create a second hash keyed on the date:

$partprice{$date}{$partnbr}{$pricelistname} = $price

It's not clear to me that you need to loop through the hash at all. What your line of code implies is that you have a HoHoH and that you are directly indexing to yet the value you want, which is about as efficient as you could want!

If you have code you are having trouble with in this regard, post an example showing the issue.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Date Ranges Question
by Tanktalus (Canon) on Oct 25, 2005 at 20:05 UTC

    GrandFather, the problem seems to be this:

    %partprice = ( 1 => { '2005-01-01' => 1.99, # new year special. '2005-01-02' => 8.99, # back to normal. '2005-02-15' => 9.99, # price bump. '2005-04-01' => 9.49, # on sale. '2005-04-08' => 9.99, # sale's over. }, );
    The question from this is: what is the cost of item #1 for a purchase made on March 18th, 2005? For that, you have to scan through looking for the last date key that is less than or equal to the desired date.

    If, however, you had an ordered hash, you could do a binary search for it. Or if you had a tree or something where the date keys were stored in an order, you could look it up faster.

      That is exactly the problems. Thanks Tanktalus ;-)