Link to home
Start Free TrialLog in
Avatar of rmacmich
rmacmich

asked on

Sorting a directory by last modified date

In UNIX, using Perl, what would be the code to get a directory listing (with no traversing subdirectories) of files with a particular extension, sorted by last date modified and stored in an array?

Thanks!
Avatar of ozo
ozo
Flag of United States of America image

@array = sort {-M $a <=> -M $b} <*.ext>
ASKER CERTIFIED SOLUTION
Avatar of amitpagarwal
amitpagarwal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thoellri
thoellri

ozo,

that's supposed to be
@array = map { $_->[0] }
  sort { $a->[1] <=> $b->[1] }
  map { [$_, -M] }
  <*.ext>;

you posted the schwartzian transform to one of my comments a long time ago.

Tobias
#or using a generic Schwartzian Transform:
sub ST(&@){
        my $metric=shift;
        map {$_->[0]}
        sort {$a->[1] cmp $b->[1]}
        map {[$_,&{$metric}]} @_
}

@array = ST {pack'N',(stat)[9]} <*.bak>;
ozo, I have posted a new question for you in this topic area.

darinw
Customer Service