473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reading a Windows dir into an array, sorting by date, grabbing most recent

2
Hello,
I'm trying to do a Perl script on Windows (wish it was Unix) that goes to a directory, figures out which files are most recent, within the last week, then copies them to another dir. I'm stuck at the sorting of the files in a directory. This part of the code returns a list but it's not sorted by date:

Expand|Select|Wrap|Line Numbers
  1. opendir(INPUTINFO, ".") or die "Doesn't work: $!";
  2.    my @xmlfiles = grep /\.xml$/, readdir INPUTINFO;
  3.    my@sorted = 
  4.     map {join'/',(split/-/)[1,2,0]}
  5.     sort
  6.     map {join'-',(split'/')[2,0,1]}
  7.         qw(12/31/2000 12/26/2000 01/01/2001), @xmlfiles;
  8.        print "$_\n" for @xmlfiles;
  9.        print "\n";
I'm guessing it's the sort. Would file::stat work better?
Thanks,
TEVO
Aug 20 '08 #1
3 3073
nithinpes
410 Expert 256MB
To sort the files based on modified time/date, you can make use of stat() function :
Expand|Select|Wrap|Line Numbers
  1. print "$_\n" foreach(sort {(stat($a))[9]<=>(stat($b))[9]} @xmlfiles);
  2.  
Aug 21 '08 #2
KevinADC
4,059 Expert 2GB
To sort the files based on modified time/date, you can make use of stat() function :
Expand|Select|Wrap|Line Numbers
  1. print "$_\n" foreach(sort {(stat($a))[9]<=>(stat($b))[9]} @xmlfiles);
  2.  
Yes, but that is not very efficient since it will have to stat() files more then one time. Better to build an array or hash of sort keys first, then sort the keys.
Aug 21 '08 #3
nithinpes
410 Expert 256MB
Yes, but that is not very efficient since it will have to stat() files more then one time. Better to build an array or hash of sort keys first, then sort the keys.
Agreed. Thanks for the update Kevin :)

Expand|Select|Wrap|Line Numbers
  1.  $stathash{$_} = (stat($_))[9] foreach(@xmlfiles);
  2.  print "$_\n" foreach(sort {$stathash{$a}<=>$stathash{$b}} keys %stathash);
  3.  
Aug 21 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
32
by: cat_dog_ass | last post by:
I am used to programming in DOS mode via Borland C++. I would now like to create programs for the Windows envirinment. Is it absoultely necessary to use Visual C++ to do this? Are there other tools...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
4
by: Stefan | last post by:
What sorting-algorithm is Array.Sort based upon? Is it an effective way of sorting an array? Regards, Stefan
3
by: able | last post by:
Dear friends I have loaded a huge amount of data in an two domensional array. I need to do a lot searching in the array and sorting the array. My question is if an array is the most apropriate...
1
by: schmud | last post by:
i'm having trouble correctly sorting dates in my dataview. It will sort the column but it just sorts according to the month number. I need to display the data on my companies site with the most...
9
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt,...
2
by: darrel | last post by:
We have a home grown web site. We store page information, including a time-stamp as to when it was last updated in a database table. We also store the site architecture (navigation) in an XML...
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.