An old tactic when faced with a representation that has a lot of boundary cases. Convert to and from a representation that is easier to work with, like so:
use strict; use Carp; # Takes a list of months in yyyymm format, returns the gaps in sorted +order. sub find_gaps { my @dates = sort {$a <=> $b} map {yyyy_mm2mon_num($_)} @_; my $last_mon_count = shift @dates; my @gaps; foreach my $mon_count (@dates) { foreach my $missed (($last_mon_count + 1)..($mon_count-1)) { push @gaps, mon_num2yyyy_mm($missed); } $last_mon_count = $mon_count; } return @gaps; } # Converts a count of months back into yyyymm format sub mon_num2yyyy_mm { my $mon_num = shift; use integer; my $yyyy = $mon_num / 12; my $mm = 1 + $mon_num % 12; # Sanity check unless (1900 < $yyyy and $yyyy < 2200) { carp("Month number '$mon_num' gives a year of $yyyy which seems st +range"); } return "$yyyy-$mm"; } # Converts yyyymm format into a count of months from year 0. (Which d +idn't # exist, shoot me. It is a convenient base-point for calculations.) sub yyyy_mm2mon_num { my $date = shift; if ($date =~ /(\d{4}).*?(\d\d?)/) { return 12*$1 + $2 - 1; } else { confess("Date '$date' is not in yyyymm format?"); } }

In reply to Re (tilly) 1: Finding gaps in date ranges by tilly
in thread Finding gaps in date ranges by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.