in reply to sort array by part of the string
The canonical tool for sorting by a temporary (and possibly expensive to calculate) key is the Schwartzian Transform (See answers to When does it pay to use the schwartzian transform? and What is "Schwarzian Transform" (aka Schwartzian) or Super Search it yourself).
The guts of the technique is to use map to generate a temporary array containing the original (unsorted) data and a key string. Use sort to sort the array by the key string, and another map to translate the sorted array back into the original (but now sorted) form.
The code looks something like this:
use strict; use warnings; my @array = qw(one4wobble two3wibble three2wig four1wog); @array = map {$_->[1]} # Convert back to original form sort {$a->[0] <=> $b->[0]} # Sort on the generated key map {[(/(\d+)/ ? $1 : -1), $_]} # Generate [key, data] pairs @array; print "@array";
Prints:
four1wog three2wig two3wibble one4wobble
|
|---|