[Kc] sort by mod date

jyoung79 at kc.rr.com jyoung79 at kc.rr.com
Thu Jun 10 22:35:17 CDT 2004


Hello,

I'm new to Perl and I was just curious if anyone here had any thoughts 
about the code below.  I'm running this on OS X (currently using an app 
called 'Affrus') and it sorts the files by modification date.  Here's 
my original code:

-------
#!/usr/bin/perl

my $dir = "/Users/jay/Desktop/Other Stuff/old stuff 4";

opendir FOLDER, $dir or die "Cannot open $dir: $!";

foreach $file (readdir FOLDER) {
    next if $file =~ /^\./;
    $path = "$dir/$file";
    next unless -f $path and -r $path;
    push @files, (stat $path)[9].chr(1).$path."\n";
}

@files = sort @files;

foreach (@files) {
    push @sortedList, (split(chr(1), $_))[1];
}

print @sortedList;
-------

I asked this question also on the OS X Perl List and Jarkko had sent 
this:

-------
opendir(DIR, shift || ".") && print map { "$_->[1]\n" } sort { $a->[0]
<=> $b->[0] || $a->[1] cmp $b->[1] } map { [ (stat($_))[9], $_ ] } grep
{ !/^\.{1,2}$/ } readdir(DIR)
-------

which I thought looked really cool, so I changed it a bit to:

-------
opendir(DIR, shift || "/Users/jay/Desktop/Other Stuff/old
stuff 4") && print map { "$_->[1]\n" } sort { $a->[0] <=>
$b->[0] || $a->[1] cmp $b->[1] } map { [ (stat($_))[9], $_ ]
} grep { !/^\.{1,2}$/ } readdir(DIR)
-------

but I never could make it work.  It kept giving me these errors:

•  Argument (file name) isn't numeric in numeric comparison (<=>)
•  (62) Use of uninitialized value in string comparison (cmp)

Anyway, if anyone has any thoughts or insights with this, I'd love to 
hear it.

Thanks.

Jay




More information about the kc mailing list