For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Get date from inside a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Get date from inside a file
Mike

2006-11-01, 6:57 pm

I'm trying to grab a couple dates that are contained in a file. I'd
like to put these dates into a couple variables, so I can test for the
highest (latest) and lowest (earliest) dates.

My perl script currently parses log files in a directory, and it does
some simple addition on some of the other data in the files.

Each file begins with a line for a start date and an end date, and I
would like to grab those dates and throw them in a variable.


--- Example File 1 ---

Start date: 20061101
End date: 20061102

[ A bunch of other data ]
--- End Example File 1 ---

--- Example File 2 ---

Start date: 20061030
End date: 20061031

[ A bunch of other data ]
--- End Example File 2 ---

So far, I've got the if loops that will test dates to see which is the
highest and lowest, where $edatefinal and $sdatefinal are going to be
highest and lowest values, and $edatevalue and $sdatevalue are going to
hold the variable pulled from each of the files for testing:

if ($edatefinal < $edatevalue) {
$edatefinal = $edatevalue;
}
if ($sdatefinal > $sdatevalue) {
$sdatefinal = $sdatevalue;
}

Thanks

Mike

Mike

2006-11-01, 6:57 pm


Mike wrote:
> I'm trying to grab a couple dates that are contained in a file. I'd
> like to put these dates into a couple variables, so I can test for the
> highest (latest) and lowest (earliest) dates.
>
> My perl script currently parses log files in a directory, and it does
> some simple addition on some of the other data in the files.
>
> Each file begins with a line for a start date and an end date, and I
> would like to grab those dates and throw them in a variable.
>
>
> --- Example File 1 ---
>
> Start date: 20061101
> End date: 20061102
>
> [ A bunch of other data ]
> --- End Example File 1 ---
>
> --- Example File 2 ---
>
> Start date: 20061030
> End date: 20061031
>
> [ A bunch of other data ]
> --- End Example File 2 ---
>
> So far, I've got the if loops that will test dates to see which is the
> highest and lowest, where $edatefinal and $sdatefinal are going to be
> highest and lowest values, and $edatevalue and $sdatevalue are going to
> hold the variable pulled from each of the files for testing:
>
> if ($edatefinal < $edatevalue) {
> $edatefinal = $edatevalue;
> }
> if ($sdatefinal > $sdatevalue) {
> $sdatefinal = $sdatevalue;
> }
>
> Thanks
>
> Mike


I guess I should have added, that I have $edatefinal and $sdatefinal
variables set:

my $edatefinal = 0;
my $sdatefinal = 999999999;

Thanks

Mike

Mike

2006-11-01, 6:57 pm


Mike wrote:
> Mike wrote:
>
> I guess I should have added, that I have $edatefinal and $sdatefinal
> variables set:
>
> my $edatefinal = 0;
> my $sdatefinal = 999999999;
>
> Thanks
>
> Mike


Ahh, after a couple hours, I think I finally got it:

if ($_ =~ /(Start\D+:)\s+(\d+)/) {
$sdate = $1;
$sdatevalue = $2;
if ($sdatefinal > $sdatevalue) {
$sdatefinal = $sdatevalue;
}
}
if ($_ =~ /(End\D+:)\s+(\d+)/) {
$edate = $1;
$edatevalue = $2;
if ($edatefinal < $edatevalue) {
$edatefinal = $edatevalue;
}
}

Does this look okay?

Thanks

Mike

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com