in reply to Finding gaps in date ranges

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";

Replies are listed 'Best First'.
Re: Re: Finding gaps in date ranges
by MeowChow (Vicar) on May 11, 2001 at 09:11 UTC
    I am absolutely baffled by MeowChow solutions. What the heck does +pop do?
    Sorry {g} I should have put my golf clubs away :) The +pop simply removes the first argument from @_, which is the hash reference, and the %{...} around it dereference the hash ref. The + is there because the dereference by the enclosing %{...} is otherwise ambiguous. Without it, Perl would assume you are just referencing a hash variable named %pop, since the characters between the braces would all be [a-zA-Z0-9_ ]. Hmm... I hope that made some semblence of sense.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print