in reply to How Many Mondays in Date Range?
Update: There. fixed. Except now its slower :( though if you unroll that sub reference/dereference and map into two separate maps then its faster :)use strict; # Days start at 0, just like arrays my $start_day = 6; my $days = 7; my @cnts = daycounts($start_day, $days); for (0..6) { print "$_: $cnts[$_]\n"; } sub daycounts { use integer; my $start_day = shift; my $days = shift; my $end_day = ($start_day + $days) % 7; my $compare = ($start_day <= $end_day) ? sub { my $num = shift; $start_day <= $num and $num <= $end_day } : sub { my $num = shift; $num <= $end_day or $start_day <= $num }; my @adjust = map { $compare->($_)? 1 : 0 } 0..6; my $cnts = $days / 7; map { $cnts + $adjust[$_] } 0..6; }
Another update: Re:ThuG's reply - I made the assumption that zero days means just the start day, and 1 day means the start day and the next day, so 17 days from Wednesday would take you to Saturday, not Friday. So just subtract one from $days coming in to get what you expect.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Runrig's Solution RE: How Many Mondays in Date Range?
by THuG (Beadle) on Aug 02, 2001 at 18:13 UTC |