Since your date field is alpha-sortable, there is no point in parsing, splitting and converting all the dates in your file. It is far simpler and much faster to convert the target date (45 days ago) to the same format:
my @bits = split ' ', localtime( time() - ( 45 * 24 * 60 * 60 ) );; my $n = 1; my %months = map{$_, $n++} qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct +Nov Dec];; my $targetDate = join '-', $bits[ 4 ], $months{ $bits[ 1 ] }, $bits[2] +;; print $targetDate;; 2007-11-28
You can now just use a string compare to select the records and avoid the splits and conversions.
This pseudocode assumes that the records in your DB file are ordered correctly. It also assumes that the last field of each record is always 4 digits:
open OLDDB, '<', $dbname or die ...; open NEWDB, '>', $tempfile or die ...; open ARCHIVE, '>>', $archive or die ...; print ARCHIVE while defined( $_ = <OLDDB> ) and substr( $_, -17, 10 ) lt $targetDate; print NEWDB; ## Output first 'failing' record to newdb print NEWDB while <OLDDB>; close for OLDDB, NEWDB, ARCHIVE; unlink $dbname; rename $tempfile, $dbname;
Even if the above assumptions are incorrect, it will still be quicker to convert the target date to a string once, than convert every record date to an integer.
In reply to Re: Extracting Line from Text file over 45 days old
by BrowserUk
in thread Extracting Line from Text file over 45 days old
by chris654
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |