Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I sort a file-listing?

Posted on 1998-10-21
6
Medium Priority
?
235 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
[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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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

705 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