in reply to Aid for regex expression
See sort for how to create a custom sort routine.
Your spec is not clear but here I am assuming:
So, use substr and work from the end, since you don't know know how many characters are in the "name", but you do know for the extension and the four-digit numeric part.
Here I sorted the lines manually as described, for the source. Then rearranged them, sorted them with the custom subroutine, and used Test::More to check the results. I also added a couple of entries to show the final sort criterion.
Output:use strict; use warnings; use Test::More tests => 1; use List::Util 'shuffle'; chomp( my @lines = <DATA> ); my @shuffled = shuffle @lines; my @sorted = sort my_sort @shuffled; is "@sorted", "@lines", 'same order'; sub my_sort { substr( $a, -9, 1 ) cmp substr( $b, -9, 1 ) || substr( $a, -8, 4 ) <=> substr( $b, -8, 4 ) || substr( $a, 0, (length $a) - 8 ) cmp substr( $b, 0, (length $b) - +8 ) } __END__ Barneym0010.111 Fredm0010.111 namem0010.111 namem0900.111 namem1100.111 namep0000.111 namep0800.111 namep9999.111
$ perl 1206433.pl 1..1 ok 1 - same order
Update: Added sorting by "name" after realizing the files are probably not actually named "name*" :-P
Hope this helps!
|
|---|