Here is an update that does exactly as you ask (Monday = 1 and Sunday = 7) and iterates a maximum of 6 times - ie it adds initialises the array with int($days/7) and just adds the remaining 0-6 to the appropriate elements. This is as fast and efficient as it gets.

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

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.