First off, I'm sorry if there is some module that will do this for me. I'm not very good at searching CPAN. Secondly, my search here didn't help with my particular question. Not saying this hasn't been asked here, just saying I didn't have much luck (or patience) in finding it.

I want to know how many Mondays and Tuesdays and all are in a given date range. So far my mind has worked up the following code. I'm wondering if there is a quicker way or a module way of getting this.

my $days; # This is set to the number of days in the range. # However you get this, it is just end date - start # date in days. my $firstday; # This is a number (1-7) telling which day of the # week the range starts on. For example, if the first # day is today (Tuesday), then this is set to 2. my @daycounts; # This will be an array with seven elements, each one # holding the number of occurances of that day in the # range for(my $i=1; $i<8; $i++){ if($i<$firstday){ $daycounts[$i]=$days/7; } else { $daycounts[$i]=($days+$firstday-$i)/7; if (($days+$firstday-$i)%7>0) { $daycounts[$i]++; } } }

'Course, you can rearrange the ifs if you like. You could do all of that work with one if( || ). As far as I know that code should work. I just don't know if it is the best.

I was also thinking there was a division operator that would only return the interger part dropping any remainder. I thought it was \, but I'm not finding doco for that in Perl, so I must be thinking a different language. I would've used \ in place of all of the /s. I'll have to find some way to cast the result of the divisions to integers instead of floats, but that is easy.


In reply to 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.