in reply to How Many Mondays in Date Range?

Once again we use day 0-6. I have added a hash to show the output in a nice format. Oh you can do it in a one liner as shown :-). The advantage of 0-6 for the days is that we can use modulus to good effect to generate our 7 indexes (0-6) for the days. You could always copy element 0 of the day count array to element 7 if you are really set on having Sunday as day 7 or add 1 to the mod and iterate from ($firstday-1)..($days+$firstday-2) but you do waste element 0 in your array.

my $days = 71; my $firstday = 5; my @daycounts = (0) x 7; # initialise 7 array elements to 0 for my $day_num( $firstday .. ($days+$firstday-1) ) { $daycounts[$day_num%7]++; } # now print it out nice and pretty my %weekdays = ( 1 => Monday, 2 => Tuesday, 3 => Wednesday, 4 => Thursday, 5 => Friday, 6 => Saturday, 0 => Sunday); for my $day_num(0..6) { print "$daycounts[$day_num]\t$weekdays{$day_num}\n"; } # here it is as a one liner $daycounts[$_%7]++ for $firstday..($days+$firstday-1);
cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Tachyon's Solution RE: How Many Mondays in Date Range?
by THuG (Beadle) on Aug 02, 2001 at 18:19 UTC

    In this, I am getting the answer I expect. As I said before, I'm using Date::Manip, which is why I'm using 1..7. But I can +/- 1 as necessary, though I do need to make Monday 0 and Sunday 6. I like the one liner, since this is going to be put into an ASP, that helps.

    Though you solution does require it step through the date range. That can be annoying for very large date ranges.

    -Travis