I'm writing a script in Perl that I want to run on all the .csv
files in a given directory. The names of the files are of the type: CCCC0.csv, CCCC1.csv, ..., CCCC198.csv
. However, I want Perl to first run the script on file CCCC0.csv
, than on CCCC1.csv
etc...So, basically, according to the increasing value of the number at the end of the file name.
If I write:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $file;
my @files = <*.csv>;
my @orderedfiles = sort @files;
for $file (@orderedfiles) {
... do stuff
}
it first runs on CCCC100.csv
rather than CCCC11.csv
while if i write
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $file;
my @files = <*.csv>;
my @orderedfiles = sort { substr($a, 4) <=> substr($b, 4) } @files;
for $file (@orderedfiles) {
... do stuff
}
it gives me an error telling me that I'm not ordering a numeric (I assume that he doesn't understand that it's a number after the 4 characters rather than another character.) I have looked at the countless questions on Stackoverflow or perlmonks that deal with sorting but i haven't been able to find an answer to my question.
EDIT: I'm using a windows machine.
ls *.csv | xargs -d '\n' yourscriptname
or similar.