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


In reply to Re: How Many Mondays in Date Range? by tachyon
in thread How Many Mondays in Date Range? by THuG

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.