Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: sorting files by date

by Thelonius (Priest)
on Feb 06, 2007 at 16:18 UTC ( [id://598570]=note: print w/replies, xml ) Need Help??

in reply to sorting files by date

One thing that everybody here is forgetting is that unless you are doing a readdir on the current directory, you must prepend the directory name for -M, -C, stat, etc.

Here are some ways to do it:

#!perl -w use strict; use File::stat; # Method 1: my $input_directory_lowest = "pridir"; opendir (LOWEST, $input_directory_lowest) or die "Error opening directory: $!"; my @files; while (my $fname = readdir LOWEST) { if ($fname =~ /^Flat_file.*\.txt$/) { push @files, [stat("$input_directory_lowest/$fname")->ctime, $fname]; } } closedir(LOWEST); my @low1 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} @files; print join("\n ", "low1=", @low1), "\n"; # Method 2: same as 1, but without explicit loop opendir (LOWEST, $input_directory_lowest) or die "Error opening directory: $!"; my @low2 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} map { [ stat("$input_directory_lowest/$_")->ctime, $_ ] } grep /^Flat_file.*txt$/, readdir(LOWEST); closedir(LOWEST); print join("\n ", "low2=", @low2), "\n"; # Method 3: outputs file names with directory; uses 'glob' my @low3 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} map { [ stat($_)->ctime, $_ ] } glob("$input_directory_lowest/Flat_file*.txt"); print join("\n ", "low3=", @low3), "\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://598570]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-05-22 15:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found