in reply to Re^2: Sort problems
in thread Sort problems

Thanks, but still it didnt do it right. Take this list for example, the result:
buff1_100u[1] buff1_12p5u[0] buff1_25u[6] buff1_50u[4] buff2_100u[3] b +uff2_12p5u[2] buff2_25u[3] buff2_50u[1]
buff1_100u1 cant come before buff1_25u6...

Replies are listed 'Best First'.
Re^4: Sort problems
by ikegami (Patriarch) on Dec 11, 2008 at 17:20 UTC

    For some reason, I thought the leading zero would address that. What was I thinking? Moving on...

    map { ( my $s = $_ ) =~ s/(\d+)/ sprintf('%010d%s', length($1), $1) /eg; [ $_, $s ] } }

    One could use pack instead of sprintf, but it won't work with unicode semantics. That's already a problem, but it'll be even worse in 5.12 because I believe unicode semantics will be used for all strings instead of just those internally encoded as UTF-8 (by default).

      It worked great! Thanks! One more thing, what if I want to sort the numbers inside the brackets from top to bottom? I mean, the same sort you did for the word but opposite sort to the numbers in the brackets:
      a[2] a[1] b[1] b[0]
        map { ( my $s = $_ ) =~ s/(\d+)/ sprintf('%010d%s', length($1), 4294967296-$1) /eg; [ $_, $s ] } }