Solved

How do I sort a file-listing?

Posted on 1998-10-21
6
177 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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