I call mine Abusing substr. The idea: convert it to zero padded month and then use substr to walk the date range from the first to the last, pushing onto the array ones you don't have. I hate having to convert it back to YYYY-M+ for testing.

I am absolutely baffled by MeowChow solutions. What the heck does +pop do?

#!/usr/bin/perl -w use strict; sub find_gaps { my $dates=shift; my @dates=sort map { sprintf "%04d-%02d",split/-/ } keys %$dates; my ($c,$l)=($dates[0],$dates[$#dates]); my @gaps; while ($l cmp $c) { my $r=sprintf("%d-%d",split(/-/,$c)); push @gaps,$r unless defined $dates->{$r}; substr($c,5,2)++; if (substr($c,5,2)==13) { substr($c,5,2)='01'; substr($c,0,4)++; } } \@gaps; } my %dates=map { $_,'' } qw ( 2000-4 2000-2 2000-1 2000-12 2001-1 2002- +12 1999-12 ); my $gaps=find_gaps \%dates; print "dates\n",join("\n",sort keys %dates),"\n"; print "gaps\n",join("\n",@$gaps),"\n";

In reply to Re: Finding gaps in date ranges by mr.nick
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.