in reply to date as part of filename

Since your dates are already in a format that is easily sorted, just use a simple '>' comparison to the one week ago date value.

use DateTime; use strict; use warnings; my $oneweekago = DateTime->now()->subtract(days => 7)->strftime("%Y%m% +d"); while (<DATA>) { print if /(\d{8})/ && $1 > $oneweekago; } =prints abcd3.20110509-2345 =cut __DATA__ abcd1.20110429-2345 abcd2.20110502-2345 abcd3.20110505-2345 abcd3.20110509-2345

Update: Changed implementation from POSIX to DateTime per ikegami

Replies are listed 'Best First'.
Re^2: date as part of filename
by ikegami (Patriarch) on May 12, 2011 at 22:58 UTC
    I can't find anything that says the time components can be negative. Any idea what POSIX (the spec, not the module) says?

      Good Question, it's an undocumented feature that I've simply observed others using from time to time.

      However, testing it shows that it works across month boundaries, but fails on year boundaries

      use POSIX qw(strftime); use strict; use warnings; my @date = localtime; while (<DATA>) { my ($year, $mon, $day, $expected) = split; @date[3,4,5] = ($day, $mon, $year); my $fmt = strftime "%Y%m%d", @date; print "$fmt <=> $expected " . ($fmt eq $expected ? 'matched' : 'fa +iled') . "\n"; } =prints 20110501 <=> 20110501 matched 20110430 <=> 20110430 matched 20110429 <=> 20110429 matched 20110102 <=> 20110102 matched 20110101 <=> 20110101 matched 20110512 <=> 20101231 failed 20110512 <=> 20101230 failed =cut __DATA__ 111 4 1 20110501 111 4 0 20110430 111 4 -1 20110429 111 0 2 20110102 111 0 1 20110101 111 0 0 20101231 111 0 -1 20101230

      Truth is, using DateTime is cleaner anyway, so will update my script.

        Thanks for the reply. I wont be able to work with the modules. So would have to use localtime Thanks a lot for your help