> -----Original Message-----
> From: Mihir Kamdar [mailto:[email protected]]
> Sent: Tuesday, August 18, 2009 08:55
> To: Steve Bertrand
> Cc: beginners
> Subject: Re: script to compare dates
>
> I dont have Datetime module installed. Is there a way without using
> DateTime??
>
You don't need it. You can with a little work generate what you
need. I don't have access to an open setup, so here is a shot at using
what you already have in a normal load. You will need to modify and
possibly add some checks, but all doable using std libs:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local 'timelocal';
my %MonthNameToNbr = qw( JAN 0 FEB 1 MAR 2 APR 3 MAY 4 JUN 5 JUL 6
AUG 7 SEP 8 OCT 9 NOV 10 DEC 11);
my $checkdateinsecs = timelocal(0, 0, 0, 31,
$MonthNameToNbr{q[mar]}, 109); # generate check date once
printf "checkdateinsecs: %d\n",
$checkdateinsecs;
my $day;
my $month;
my $year;
my @cdr = ();
while ( <DATA> ) {
chomp;
next if ( /^\s*$/ );
@cdr = split(/\s*,\s*/, $_);
if ( $cdr[0] !~ /^.(\d+)\S(\S{3})\S(\d+)/ ) {
#print some type of error and either continue or die
printf "(%5d)in error:\n<%s>\n",
$.,
$_;
next;
}
$day = $1;
$month = uc($2);
$year = $3;
my $dateinsecs = timelocal(0, 0, 0, $day,
$MonthNameToNbr{$month}, $year+100);
next if ( ! ( ($dateinsecs > $checkdateinsecs ) and
$cdr[1] =~ /2.*/
)
);
# do your other processing
printf "dateinsecs: %d was greater (%d)\n",
$dateinsecs,
$.;
}
__DATA__
*04-NOV-08*,2
*04-apr-09*,3
*xx-dec-08*,2
*04-apr-09*,2
Output:
[C:/CurrWrka/00Commonprlprod/src] aapl013s
checkdateinsecs: 1233385200
( 3)in error:
<*xx-dec-08*,2>
dateinsecs: 1238824800 was greater (4)
> On Tue, Aug 18, 2009 at 7:37 AM, Steve Bertrand
> <[email protected]> wrote:
>
> > Mihir Kamdar wrote:
> > > Hi,
> > >
> > > I want to write a script whose input data would be a csv
> file and records
> > > would be as follows:-
> > >
> > > 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*
> > > ,10000,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
> > > 30000000026495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*
> > > ,100000,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> > > 30000000027480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*
> > > ,100000,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> > >
> > > Here I want to compare whether the 5th field, which is
> the date field
> > > is *earlier
> > > than 31-Mar-09 and 2nd field value is 2.*
> > >
> > > If yes, then I will take that record and store it in another file.
> > >
> > > Please help me as to how do I compare dates, preferably
> with some sample
> > > code.
> > >
> > > I started coding for this as below, but am stuck on how
> to compare date
> > in
> > > my input with another date.
> > >
> > > #!/usr/bin/perl
> > > use strict;
> > > use warnings ;
> > >
> > > open (my $IN_FILE,"<","testdata.txt") or die $!." file
> not found" ;
> > > while (my $line=readline($IN_FILE))
> > > {
> > > my @cdr=split (/,/, $line) ;
> > > if($cdr[5]
> > > .............
> > > .............
> > > }
> >
> > For fun, and to try out a few things I came across in
> Damian's book. I
> > use DateTime to manage all aspects of dates and times. The
> code below
> > assumes that all records will always be in the exact same format.
> > Criticism welcome:
> >
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > use DateTime::Format::Strptime;
> >
> > my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';
> >
> > VOUCHER_RECORD:
> >
> > while ( my $record = <DATA> ) {
> >
> > my ( $important_num, $date_string )
> > = unpack $EXTRACTION_LAYOUT, $record;
> >
> > next VOUCHER_RECORD if $important_num != 2;
> >
> > my $static_date = DateTime->new(
> > year => 2009,
> > month => 3,
> > day => 31
> > );
> >
> >
> > # turn the extracted date string into a DateTime object
> >
> > my $date_formatter
> > = new DateTime::Format::Strptime( pattern => '%d-%b-%y' );
> >
> > my $voucher_date
> > = $date_formatter->parse_datetime($date_string);
> >
> >
> > # compare the DateTime compiled dates
> >
> > if ( DateTime->compare( $static_date, $voucher_date )) {
> >
> > print "Voucher ${voucher_date} is prior to ${static_date}" .
> > ".... and the important number is 2\n";
> > }
> > }
> >
> > __DATA__
> >
> >
> 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*,10000,INR,,
VOUCHER_BATCH_20080919_00014,2C,,0
> >
> >
> 30000000026495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*,100000,INR,
,VOUCHER_BATCH_20080919_00024,1K,,0
> >
> >
> 30000000027480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*,100000,INR,
,VOUCHER_BATCH_20080919_00024,1K,,0
> >
> > Steve
> >
>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/