Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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";

In reply to Re: sorting files by date by Thelonius
in thread sorting files by date by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found