Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I sort a file-listing?

Posted on 1998-10-21
6
Medium Priority
?
238 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 85

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 85

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 85

Accepted Solution

by:
ozo earned 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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

885 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