Solved

How do I sort a file-listing?

Posted on 1998-10-21
6
196 Views
Last Modified: 2010-03-05
I have a script that uses:
  while ($filename = readdir DIR) {
to display a directory's contents. Is there anyway to sort these results in a particular order (i.e. by date?)
0
Comment
Question by:drummond
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205596
sub ST(&@){
         my $metric=shift;
         map {$_->[0]}
         sort {$a->[1] cmp $b->[1]}
         map {[$_,&{$metric}]} @_
}

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
for $filename ( ST{(stat"$some_dir/$_")[9]} readdir DIR ){
      print $filename." ".localtime((stat"$some_dir/$filename")[9])."\n"
}

0
 

Author Comment

by:drummond
ID: 1205597
That made it work REALLY well. Now how do I reverse it so that the most recent files are at the top of the list?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205598
for ( reverse ST{(stat"$some_dir/$_")[9]} readdir DIR ){
}
#or
for ( ST{ ~(stat"$some_dir/$_")[9] } readdir DIR ){
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:drummond
ID: 1205599
Everything works perfectly! Now how do I give you the points?
0
 
LVL 2

Expert Comment

by:WileyKat
ID: 1205600
to get the points, ozo should post his answer as an answer instead of a comment. you can then give him the points.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1205601
sub ST(&@){
        my $metric=shift;
        map {$_->[0]}
        sort {$a->[1] cmp $b->[1]}
        map {[$_,&{$metric}]} @_
}

for( ST{ ~(stat"$some_dir/$_")[9] } readdir DIR ){ }
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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