Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting readdir results by date

Posted on 2002-05-16
4
Medium Priority
?
1,619 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

604 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