in reply to best practices for a complex sort + splitting an alphanumeric string
Well, #3 is pretty easy to answer.
# given the string value in $_ ... my @v = split m#/#; unshift @v, shift(@v) =~ /^(.*?)(\d*)$/;
For values that don't have any letters at the beginning, the first element of @v will be an empty string.
From there, you can easily create a sort key, using (for example)
Putting it all together:sprintf "%-4s%3d%3d%3d", @v;
It's probably worth pointing that this could be further optimized by using the GRT, or possibly fast, flexible, stable sort. See also this tutorial on sorting.map { $_->[1] } sort { $_->[0] cmp $_->[1] } map { my @v = split m#/#; unshift @v, shift(@v) =~ /^(.*?)(\d*)$/; no warnings; # in case @v has less than 4 elements [ sprintf( "%-4s%3d%3d%3d", @v ), $_ ] }
|
|---|