http://www.perlmonks.org?node_id=621961

Scrat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone

I have a list of xml files in 'n directory:

WAN_DX_ACD_ACD_2007_06_10_00_10_38_042.csv.xml WAN_DX_ACH_ACH_2007_06_13_00_10_37_051.csv.xml WAN_DX_ADY_ADY_2007_06_10_00_10_37_060.csv.xml WAN_DX_ALD_ALD_2007_06_10_00_10_38_073.csv.xml WAN_DX_ALE_ALE_2007_06_10_00_10_39_106.csv.xml WAN_DX_BFN_BFP_2007_06_11_00_15_52_400.csv.xml WAN_DX_BNA_BNA_2007_06_30_00_22_32_641.csv.xml WAN_DX_BLV_BLV_2007_06_22_00_22_34_667.csv.xml
The above filenames contain the following (taking the first file as an example):
WAN_DX_ACD_ACD - This is the network device name
2007_06_10 - This is the date (yyyy-mm-dd)
00_10_38_042 - This is the time including milliseconds(00:10:38:042 AM)

I need to sort these files descendingly according to date and then time (process the oldest files first, and the newest files last). This is my code, which doesn't work:

#!/usr/bin/perl use strict; use warnings; my $dir = "D:\\scripts\\"; my (@sorted_list, @file_list); if ( opendir(DIR, "$dir") ) { foreach my $file( readdir(DIR) ) { next if ( $file =~ /^\./ ); if ($file =~ /xml$/) { foreach ($file) { push (@file_list, $_); } } } @sorted_list = map {$_->[0]} sort { $b->[1] <=> $a->[1] } map { [ +$_,(split/\D/)[15..21]] } @file_list; foreach (@sorted_list) { print "$_\n"; } } closedir (DIR);

When I print @sorted_list, the contents are still not sorted. What am I doing wrong?

Update:Fixed typo.

Update2:Just realised another mistake - I was printing the old unsorted @file_list in my initial 2nd foreach loop, and not the new, sorted @sorted_list.