in reply to Sort files descending by date

use strict; use warnings; my $SOME_DIR = '/home/scrat/scratchpad'; my $PATTERN = qr{ \A \w+ (\d\d\d\d_\d\d_\d\d_\d\d_\d\d_\d\d_\d\d\d) [\.\w]* \.xml \z }xms; opendir( DIR, $SOME_DIR ) || die "can't opendir $SOME_DIR: $!"; my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { ($_ =~ $PATTERN) && [$_, $1] } grep { ($_ =~ $PATTERN) && -f "$SOME_DIR/$_"} readdir( DIR ) ; closedir DIR; printf "%s\n", join "\n", @sorted;

Replies are listed 'Best First'.
Re^2: Sort files descending by date
by johngg (Canon) on Jun 19, 2007 at 09:50 UTC
    (\d\d\d\d_\d\d_\d\d_\d\d_\d\d_\d\d_\d\d\d)

    Perhaps it's just me but I find all those \ds confusing and would prefer to use quantifiers.

    (\d{4}(?:_\d\d){5}_\d{3})

    Also, the requirement was for a descending sort so I think

    sort { $a->[1] cmp $b->[1] }

    should be

    sort { $b->[1] cmp $a->[1] }

    Cheers,

    JohnGG

    Update: I should have read the OP more carefully, the sort required is actually ascending as fenLisesi points out.

      process the oldest files first, and the newest files last
        Whoops, sorry, missed that explanation in the OP, just went by the title.