in reply to Date Ranges Question
I haven't tested that at all (except to confirm that using "pop @array" as the hash key index does work); still I hope the idea is clear enough.my %partprice = ( PN01 => { 2005-01-01 => 29.95, 2005-01-05 => 20.00, 2005-01-19 => 29,95, 2005-03-01 => 33.50 }, PN02 => { ... }, ... ); # pick a part number and a date of sale: my $pn = 'PN01'; my $saledate = '2005-02-14'; my $price = getPrice( $saledate, $partprice{$pn} ); # do something with $price ... sub getPrice { my ( $sdate, $prices ) = @_; # 2nd param is a hashref my @keydates = sort keys %$prices; while ( @keydates and $sdate lt $keydates[$#keydates] ) { pop @keydates; } # at this point, either the sale predates the price history (@keyd +ates is empty) # or else the last element of @keydates points to the correct pric +e for that date return ( @keydates ) ? $$prices{pop @keydates} : undef; }
|
|---|