See sort for how to create a custom sort routine.

Your spec is not clear but here I am assuming:

  1. sort first by ascending m or p
  2. sort next by ascending last four digits of file name before extension
  3. sort last by the "name" in ascending alphabetical order
  4. the "name" portion will be arbitrary

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.

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
Output:
$ 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!


The way forward always starts with a minimal test.

In reply to Re: Aid for regex expression by 1nickt
in thread Aid for regex expression by kepler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.