ebony smith wrote:
> I am new to perl and need to compare some dates in a file stored in
> dd-mm-yyyy format to find out if the dates are greater than 30, 60 or 90
> days.
> However, I am not quite sure how to go about doing that in perl and I
> was wondering if anyone out there had any tips on how to do this.
> 
> 
> I extract the data required using the following to get a date and username
> 
> if (-e $FILE) { unlink $FILE ;}
> open (CHK, ">> $FILE") || die "Cannot open $FILE";
> 
>        system (`awk  '/Login/ {print \$1,\$7}' $FILE1 | sort -u  >>
> $FILE1`);
> 
> 
> Then this gives me the current date
> 
> $tm = localtime($date);
> $day = $tm->mday;
> $month = $tm->mon+1;
> $year = $tm->year+1900;
> 
> if ( $day < 10 ){
>       $day="0$day";
>   }
> 
> if ( $month < 10 ){
>       $month="0$month";
>   }
> 
>   $querydate = "$year-$month-$day";
>   return $querydate
> 
> 
> However I am not too sure how to go about comparing the date extracted
> from the data file with the current date to find out if it is greater
> 30, 60 or 90 days.


use warnings;
use strict;

use Date::Calc 'Add_Delta_Days';

my ( $today_day, $today_month, $today_year ) = ( localtime )[ 3, 4, 5 ];
$today_month++;
$today_year += 1900;

my $today_minus_30 = sprintf '%04d%02d%02d', Add_Delta_Days $today_year,
$today_month, $today_day, -30;
my $today_minus_60 = sprintf '%04d%02d%02d', Add_Delta_Days $today_year,
$today_month, $today_day, -60;
my $today_minus_90 = sprintf '%04d%02d%02d', Add_Delta_Days $today_year,
$today_month, $today_day, -90;


open CHK, '<', $FILE1 or die "Cannot open '$FILE1' $!";

my %unique;

while ( <CHK> ) {
    next unless /Login/;

    my ( $date, $username ) = ( split )[ 0, 6 ];

    $date = sprintf '%04d%02d%02d', reverse $date =~ /\d+/g;

    $unique{ $date, $username } = ();
    }

my @sorted = map [ split $; ], sort keys %unique;

for my $record ( @sorted ) {
    if ( $record->[ 0 ] > $today_minus_90 ) {
        do_something();
        }
    if ( $record->[ 0 ] > $today_minus_60 ) {
        do_something();
        }
    if ( $record->[ 0 ] > $today_minus_30 ) {
        do_something();
        }
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to