Solved

Sorting readdir results by date

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
problem with using the glob function 1 74
syslog unix file 20 76
batch script for automated email 12 119
To run CGI/perl on tomcat and connect to oracle database. 5 135
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…

789 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