2

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.

2
  • If you're using a Unix shell,I wouldn't include this logic into the script and simply use ls *.csv | xargs -d '\n' yourscriptname or similar. Commented Jan 30, 2015 at 18:45
  • Sorry, should have added that I'm using a Windows machine...
    – g_puffo
    Commented Jan 30, 2015 at 18:46

3 Answers 3

6

You were almost there... the '.CSV' is still there. You'd be better served using regex to read just numeric characters.

my @sorted = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } @files;

There is an idiom called the Schwartzian Transform that can also do this, though it takes a CS major to understand :D

my @sorted = map  { $_->[0] }             # return the sorted file names
                                          #
             sort { $a->[1] <=> $b->[1] } # sort on the numeric portion
                                          #
             map  { [$_, /(\d+)/] }       # wrap the file names in a temporary 
             @files;                      #   array with their numeric portions.

                                          # ^^ read from bottom to top ^^
2
  • Thank you: after reading the above comment I was trying to trim the last 4 characters using substr($a, 4, -4) but I couldn't make it work (surprisingly, it would work if I input the array manually, to run a test in Perl but it WOULDN'T work on the actual data files...)... However, your code did the job: thanks!
    – g_puffo
    Commented Jan 30, 2015 at 5:52
  • 1
    Odd... It works fine for me using subst as you describe, like so @order = sort { substr($a,4,-4) <=> substr($b,4,-4) } @files; and in fact, this might be preferable. It only looks at the substring in that indexed location (4, -4) so it would protect against edge cases like numbers in the first 4 characters of your filename.
    – elcaro
    Commented Jan 30, 2015 at 6:02
3

You could give Sort::Key::Natural a spin. From the synopsis:

use Sort::Key::Natural qw(natsort);

my @data = qw(foo1 foo23 foo6 bar12 bar1
              foo bar2 bar-45 foomatic b-a-r-45);

my @sorted = natsort @data;

print "@sorted\n";
# prints:
#   b-a-r-45 bar1 bar2 bar12 bar-45 foo foo1 foo6 foo23 foomatic
1
  • 1
    Thanks Jim: this is actually very powerful and is probably the closest to a general answer to my question! I will read the documentation to get a better idea of what it does!
    – g_puffo
    Commented Jan 30, 2015 at 6:05
2

I believe that the substr($a, 4) is returning "100.csv" in your example, so that you need to trim the .csv suffix off it still.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.