?
Solved

Sorting readdir results by date

Posted on 2002-05-16
4
Medium Priority
?
1,617 Views
Last Modified: 2010-08-05
Hi,

I'm reading a number of daily log files and displaying their contents. The log entries in the files are ordered by date no problem. However I would like the files to be ordered on my page so that the most recent appears at the top. When reading the filesnames from the directory using readdir is there some way of sorting the results by the last modified date on the file?

This is what I have so far:

# Open directory handle for log directory
opendir(DIR, "log") || print "open dir error: $!";

# Read the log files in the directory into array
@files = grep( /\.log$/i , readdir( DIR) );

foreach $file(@files)
{
  # Print log file name
  print "<h3>$file</h3>\n";

  # Open log file
  open (IN1,"log/$file");

  # Write log file contents
  while (<IN1>)
  {
    print "$_<br>\n";
  }

  # Close log file
  close IN1;

}
0
Comment
Question by:AndyReed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:dda
ID: 7015619
I can suggest to use stat() for getting the date and hash for storing filenames and last modified date. You can't do it with readdir() only.
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 7015675
@files =
    map{(split/ /,$_,2)[1]}
    sort
    map{sprintf"%8x %s",~(stat)[9],$_}
    grep{/\.log$/i}
    readdir DIR;
0
 
LVL 4

Expert Comment

by:dda
ID: 7015697
wow!
0
 

Author Comment

by:AndyReed
ID: 7015905
~(stat)[9] doesn't appear to be working. It returns 0 for each file. I'm afraid I don't understand the ~ Does it call stat with the last value of $_ ? If so then I don't think its working. Do you need 'use' something use this form?

Almost there, your solution works if I use
   map{sprintf"%8x %s",(stat(_$))[9],$_}
but obviously I don't get the filename in the second field.

Many Thanks,
Andy.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question