in reply to Re: Sort problems
in thread Sort problems

pack q{NNa*}, m{(\d+)_(\d+)}, $_
Note that packing with an 'N' specifier fails if either of the numeric fields exceeds 4294967295. An approach using sprintf (as mentioned in johngg's reply) can get around this.
>perl -wMstrict -le "my @list = qw{ a1_2 a1_1 a10_10 a2_10 a2_1 a2_2 a10_1 a10_2 a1_10 }; use constant WIDTH => 20; my @sorted = map { substr $_, WIDTH * 2 } sort map { sprintf '%0*4$d%0*4$d%s', m{(\d+)_(\d+)}, $_, WIDTH } @list; print for @sorted; " a1_1 a1_2 a1_10 a2_1 a2_2 a2_10 a10_1 a10_2 a10_10

Replies are listed 'Best First'.
Re^3: Sort problems
by johngg (Canon) on Dec 03, 2008 at 10:22 UTC

    Nice ++

    I had never seen the *4$ construct, used to grab arguments by position for printf/sprintf, before. Very useful. Thanks for pointing it out.

    Cheers,

    JohnGG

Re^3: Sort problems
by monarch (Priest) on Dec 04, 2008 at 03:38 UTC

    Where can I find an explanation for the extra formatting details that resulted in '%0*4$d%0*4$d%s' (I'd like to learn more about this myself). Has such support been around for a long time in Perl? Is it Perl-specific, or does the GNU C library now support such syntax?