Despite your complaints, sort { $a <=> $b } readdir(XMLDIR) works. When Perl treats the string 2384747.xml as a number (as <=> does), it is treated as having a value of 2384747 .
$ perl -wE'say 0+"2384747.xml"' Argument "2384747.xml" isn't numeric in addition (+) at -e line 1. 2384747
Of course, these warnings are a problem. The decision you made tries to delete them, but does not delete them all, because it does not take into account what readdir will return . and .. You must first delete files that you do not need.
Here are two simple solutions:
my @files = sort { no warnings 'numeric'; $a <=> $b } grep { /^(\d)\.xml/ } readdir(XMLDIR); my @files = sort { ( $a =~ /(\d+)/ )[0] <=> ( $b =~ /(\d+)/ )[0] } grep { /^(\d)\.xml/ } readdir(XMLDIR);
In this particular case, you can optimize your code:
my @files = map { "$_.xml" }
The easiest and fastest solution is to use a natural look.
use Sort::Key::Natural qw( natsort ); my @files = natsort grep !/^\.\.?/, readdir(XMLDIR);
source share