Here's a quick-and-dirty way to find gaps by counting the number of dates you know, then advancing from the first one, recording gaps until you've seen all the known dates there are to see. It assumes no leading zeros in months, and handles the edge cases of being handed an empty list, or a list with one date in it. Note the bruce-force determination of the initial date in the range.
sub find_gaps {
my $dates = shift;
my $ndates = scalar keys %$dates;
my @missing = ();
return \@missing if $ndates == 0;
# Determine the first date in the range
my $year = substr((sort keys %$dates)[0],0,4);
my $month = 1;
while ( ! exists $$dates{ "$year-$month" } ) {
do {$month = 1; ++$year} if ++$month > 12;
}
--$ndates;
# Look for gaps until we've seen each date in the range
while ( $ndates ) {
do { $month = 1; ++$year } if ++$month > 12;
if ( exists $$dates{"$year-$month"} ) {
--$ndates;
} else {
push @missing, "$year-$month";
}
}
return \@missing;
}
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.