This seems to be a little better, benchmarkwise (it assumes days are numbered 0-6):
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; }
Update: There. fixed. Except now its slower :( though if you unroll that sub reference/dereference and map into two separate maps then its faster :)

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.


In reply to Re: How Many Mondays in Date Range? by runrig
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.