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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.