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
smime.p7s
Description: S/MIME Cryptographic Signature
