I'm not sure how important the "pricelistname" is to solving the problem... do you really need that at all? Given the nice
rephrasing of the problem by
Tanktalus, what might work for you is something like this:
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;
}
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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.