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:
The above filenames contain the following (taking the first file as an example):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
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort files descending by date
by grinder (Bishop) on Jun 19, 2007 at 07:45 UTC | |
by Scrat (Monk) on Jun 19, 2007 at 09:40 UTC | |
|
Re: Sort files descending by date
by salva (Canon) on Jun 19, 2007 at 07:54 UTC | |
by ikegami (Patriarch) on Jun 19, 2007 at 13:58 UTC | |
|
Re: Sort files descending by date
by Anonymous Monk on Jun 19, 2007 at 07:50 UTC | |
|
Re: Sort files descending by date
by fenLisesi (Priest) on Jun 19, 2007 at 08:09 UTC | |
by johngg (Canon) on Jun 19, 2007 at 09:50 UTC | |
by fenLisesi (Priest) on Jun 19, 2007 at 09:53 UTC | |
by johngg (Canon) on Jun 19, 2007 at 10:08 UTC | |
|
Re: Sort files descending by date
by FunkyMonk (Bishop) on Jun 19, 2007 at 16:42 UTC | |
by ikegami (Patriarch) on Jun 19, 2007 at 18:08 UTC | |
by FunkyMonk (Bishop) on Jun 19, 2007 at 18:19 UTC |