Kevin Old wrote:
> 
> Hello everyone,

Hello,

> 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;
> }

You can do that without Date::Manip

use Time::Local;

use constant WED => 3;

use constant ONE_DAY  => 86_400;
use constant SIX_DAYS => 6 * ONE_DAY;

sub wed2tue {
    my @dates;

    my $year = (localtime)[5];
    my $start = timelocal( 0, 0, 12, 31, 11, $year - 1 );

    $start += ONE_DAY until (localtime $start)[6] == WED;

    while ( (localtime $start + SIX_DAYS)[5] == $year ) {
        my @date = (localtime $start)[4,3,5];
        $date[0]++;
        $date[2] += 1900;
        push @dates, sprintf '%02d/%02d/%04d', @date;
        $start += SIX_DAYS;

        @date = (localtime $start + SIX_DAYS)[4,3,5];
        $date[0]++;
        $date[2] += 1900;
        push @dates, sprintf '%02d/%02d/%04d', @date;
        $start += ONE_DAY;
        }

    @dates;
    }

use Tie::IxHash;
tie my %pairs, 'Tie::IxHash';
%pairs = wed2tue();


> 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:

You could use an array of arrays if that works for you.

> %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?

You should be able to modify the sub above, if not let me know.


John
-- 
use Perl;
program
fulfillment

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

Reply via email to