To compare dates in a script

C

clearguy02

Hi all,

Below is the scenario.

I have a file that has two entrees (a name and a date) seperated by a
tab. I need to get only all those names in the file if the date on the
each line is greater than a certain date, 12-31-2004.

Script
#######################
$current = "31-Dec-04";
foreach (<DATA>) {
($f, $l) = /(\w+)\t+(\w+)/;
print $f if ($l > $current);
}
__DATA__
5.5.25.50 01-Feb-05
TEST1 22-Jul-04
BSTOP 03-Sep-02
DB00004639 21-Jan-05
DB00004693 25-Jan-05
CDM_3.1.0 27-May-03
############################

I think I am failing on comparing stuff. How can I rectify the above
code?

Thanks,
Rider.
 
E

Eric Schwartz

I have a file that has two entrees (a name and a date) seperated by a
tab. I need to get only all those names in the file if the date on the
each line is greater than a certain date, 12-31-2004.

You should probably get in the habit of searching CPAN before posting
here-- that's not an insult, merely an observation that a lot of the
time, you'll find the answer there first, and save time.

In this case, you'd have found Date::Manip and Date::Calc, both of
which will do what you want, more or less.

-=Eric
 
A

A. Sinan Unur

(e-mail address removed) wrote in @c13g2000cwb.googlegroups.com:
I have a file that has two entrees (a name and a date) seperated by a

Just FYI: ITYM "entries".
tab. I need to get only all those names in the file if the date on the
each line is greater than a certain date, 12-31-2004.

Script
#######################

There is usually no need for this kind of separator. If you put a
shebang line in your scripts, that serves nicely to set the script apart
from the rest of the post, especially if you end your scripts with
__END__.

use strict;
use warnings;

missing. Please read the posting guidelines for this group.
$current = "31-Dec-04";

my $current = "20041231";

for reasons that will become clear later.
foreach (<DATA>) {

This first reads the whole file. Better to use

while ( said:
($f, $l) = /(\w+)\t+(\w+)/;
print $f if ($l > $current);

So, you have two strings and you compare them using the numeric operator
'>'.

Indeed, if you had turned on warnings, perl would have told you:

Argument "31-Dec-04" isn't numeric in numeric gt (>) at a.pl line 9,
I think I am failing on comparing stuff. How can I rectify the above
code?

You could use a module for comparing dates. See CPAN if you want to go
that route. On the other hand, you could also just convert your dates to
the YYYYMMDD format and use string comparison:

#! /usr/bin/perl

use strict;
use warnings;

my $current = '20041231';
my %months = (
Jan => '01', Feb => '02', Mar => '03', Apr => '04',
May => '05', Jun => '06', Jul => '07', Aug => '08',
Sep => '09', Oct => '10', Nov => '11', Dec => '12',
);

while(<DATA>) {
chomp;
last unless $_;
my ($file, $date) = split /\s+/;
my ($day, $month, $year) = split '-', $date;
# You'll need to figure out how far back the dates go
# and adjust the if statement below accordingly
if(0 + $year <= 50) {
$year += 2000;
} else {
$year += 1900;
}
$month = $months{$month};
print "$file\n" if $current le "$year$month$day";
}

__DATA__
5.5.25.50 01-Feb-05
TEST1 22-Jul-04
BSTOP 03-Sep-02
DB00004639 21-Jan-05
DB00004693 25-Jan-05
CDM_3.1.0 27-May-03

__END__
 
G

Gunnar Hjalmarsson

I have a file that has two entrees (a name and a date) seperated by a
tab. I need to get only all those names in the file if the date on the
each line is greater than a certain date, 12-31-2004.

Script
#######################
$current = "31-Dec-04";
foreach (<DATA>) {
($f, $l) = /(\w+)\t+(\w+)/;
print $f if ($l > $current);
}
__DATA__
5.5.25.50 01-Feb-05
TEST1 22-Jul-04
BSTOP 03-Sep-02
DB00004639 21-Jan-05
DB00004693 25-Jan-05
CDM_3.1.0 27-May-03
############################

I think I am failing on comparing stuff.

Obviously.
warning: Argument "31Dec04" isn't numeric in numeric gt

You need to make things comparable in order to compare them...
How can I rectify the above code?

You can:

1) enable strictures
2) enable warnings
3) declare the variables you are using
4) use an appropriate module - I would suggest Date::parse
5) refrain from unnecessarily loading the whole file into memory
6) study "perldoc perlre" to find out that \w does not match
what you think it matches

use strict;
use warnings;
use Date::parse;
my $current = str2time('31-Dec-04');
while (<DATA>) {
my ($f, $l) = /(.+)\t(.+)/;
print "$f\n" if str2time($l) > $current;
}
 
M

mothra

Hi all,

Below is the scenario.

I have a file that has two entrees (a name and a date) seperated by a
tab. I need to get only all those names in the file if the date on the
each line is greater than a certain date, 12-31-2004.
Something like this might work. It uses DateTime

use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;

#target date
my $dt1 = DateTime->new(
year => 2004,
month => 12,
day => 31,
);

my $Strp = new DateTime::Format::Strptime(
pattern => '%d-%b-%y',
);
while (<DATA>) {
chomp;
my ( $file, $date ) = split /\s+/;
my $dt2 = $Strp->parse_datetime($date);

if ( $dt1 > $dt2 ) { print "Date from file is before\n"; }
if ( $dt1 == $dt2 ) { print "Dates are the same\n"; }
if ( $dt1 < $dt2 ) { print "Date from file is after\n"; }

}

__DATA__
5.5.25.50 01-Feb-05
TEST1 22-Jul-04
BSTOP 03-Sep-02
DB00004639 21-Jan-05
DB00004693 25-Jan-05
CDM_3.1.0 27-May-03

I hope this helps

Mothra
 
P

phaylon

clearguy02 said:
I have a file that has two entrees (a name and a date) seperated by a tab.
I need to get only all those names in the file if the date on the each
line is greater than a certain date, 12-31-2004.

Just for curiousity: Only that date? I just ask because Dec 31 is the last
day in '04, so you could just check the last two chars and put all out
which are higher than 04, like this:

#!/usr/bin/perl
use warnings;
use strict;

while( <DATA> ) {
chomp;
if( /^.+\s+[0-9]{2}-[a-z]+-([0-9]{2})$/i ) {
print "$_\n" if $1 > 4;
}
}

__DATA__
5.5.25.50 01-Feb-05
TEST1 22-Jul-04
BSTOP 03-Sep-02
DB00004639 21-Jan-05
DB00004693 25-Jan-05
CDM_3.1.0 27-May-03
__END__

hth,
phay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top