Kevin Old" wrote:
>
> I have a subroutine below that uses Date::Manip to build a hash with
> keys of the previous Wednesday to the next Tuesday range from the
> beginning of the year until today.  For instance:
>
> %pairs = (
> #didn't want to list the whole year, but you get the point
> '10/15/2003' => '10/21/2003',
> '10/22/2003' => '10/28/2003',
> );
>
> Here's my subroutine:
>
> sub wedToTues() {
>         my ($day, $month, $year) = (localtime)[3,4,5];
>         $year += 1900;
>         $month++;
>         my $beginyear = &ParseDate("$year/01/01");
>         my $today = &ParseDate("today");
>         my $firsttues = &ParseDate("$year/01/07");
>         my $endyear = &ParseDate("$year/12/31");
>
>         #print "$beginyear $endyear\n";
>
>         #Build an array of every Tuesday in the current year
>         my @dates =
> &ParseRecur("0:0:1*2:0:0:0",$firsttues,$beginyear,$today);
>
>         use Tie::IxHash;
>         tie my %pairs, "Tie::IxHash";
>
>         foreach my $date (@dates) {
>
>                 my $lastWed = &DateCalc("$date","- 6 days");
>
>                 #my $ulastWed = &UnixDate($lastWed, "%Y%m%d");
>                 #my $udate = &UnixDate($date, "%Y%m%d");
>                 my $ulastWed = &UnixDate($lastWed, "%m/%d/%Y");
>                 my $udate = &UnixDate($date, "%m/%d/%Y");
>                 $pairs{$ulastWed} = $udate;
>         }
>
> return \%pairs;
> }
>
> Yes, I understand that to adjust the variables that I pass to
> ParseRecur, but I'm not sure how to go about doing what I need.
>
> I'll try to explain as best as I can.  First, I'll be changing this
> subroutine to be Saturdays to Fridays rather than Wednesdays to
> Tuesdays.  Then based on the day it's run, I need to only get the
> previous 3 weeks and the next 4 weeks.
>
> Basically I'd like a hash (or whatever) that looks like this:
>
> %datepairs = (
> # Saturday to  Following Friday
> '10/11/2003' => '10/17/2003',
> '10/18/2003' => '10/24/2003',
> '10/25/2003' => '10/31/2003',
> '11/01/2003' => '11/07/2003',
> '11/08/2003' => '11/14/2003',
> '11/15/2003' => '11/21/2003',
> '11/22/2003' => '11/28/2003',
> );
>
>
> Can anyone offer suggestions or a more graceful way of doing this?

Hi Kevin.

This seemed like a fun exercise so I wrote the program below. It builds
an array of eight elements, each of which is a reference to a two-element
array containing the start and end dates of that week. I thought that was
more appropriate than the tied hash you were using.

Your specification wasn't exact so it may need some tweaking.

HTH,

Rob


use strict;
use warnings;

use Date::Simple;
use Date::DayOfWeek;

my $date = new Date::Simple;    # today
my $dow = dayofweek(reverse split '-', $date);    # 0 = Sunday

my $prevsat = $date - $dow - 1;    # Last Sat before today

my @dates;
my $first = $prevsat - 3*7;

for (1 .. 8) {

  my $week;

  for my $ymd ($first, $first + 6) {
    my $mmddyyyy = sprintf "%02d/%02d/%04d", (split '-', $ymd)[1, 2, 0];
    push @$week, $mmddyyyy;
  }

  push @dates, $week;
}

use Data::Dumper;
print Data::Dumper->Dump ([EMAIL PROTECTED], [qw(*dates)]);

**OUTPUT**

@dates = (
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ],
           [
             '10/04/2003',
             '10/10/2003'
           ]
         );



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to