Solved

Sorting readdir results by date

Posted on 2002-05-16
4
1,604 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
  • 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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now