THuG has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|