sub nextmonth { my($year, $month); $year = substr($_[0], 0, 4); $month = substr($_[0], 4, 2); $month++; while ($month > 12) { $year++; $month-=12; } return $year . sprintf("%02d", $month); } sub find_gaps { my $dates = shift; my(@missing, @keys, $last); # convert your date-format to mine @keys = map { s/-(.)$/0$1/; s/-//; $_ } keys %$dates; # my format can be sorted numerically @keys = sort @keys; # in the loop we need two variables: # $last for the date before the current one, # and $date for the current date $last = shift(@keys); foreach my $date ( @keys ) { while (1) { $last = nextmonth($last); last if $last eq $date; push(@missing, $last); } $last = $date; } # convert my date-format back to yours @missing = map { s/0(.)$/$1/; s/^(....)/$1-/; $_ } @missing; return \@missing; }