merrymonk has asked for the wisdom of the Perl Monks concerning the following question:
%out = (); for (@in){ if (/^([A-Za-z_]+)(\d+)$/){ # build hash of arrays # with alpha part uppercase:original as key push @{$out{join ':',uc $1,$1}},$2; } else { warn "\nInput data error <$_>\n"; } }
This gave exactly what I wanted.
As so often is the case the problem has become more complicated since I find that:
1. There can be a number or numbers in the middle as well at the end; 2. There can be some numeric values only.
Numbers in the middle case
A typical set of values when numbers are in the middle are
blank_5_str_1 blank_5_str_10 blank_5_str_11 blank_5_str_12 blank_5_str_13 blank_5_str_14 blank_5_str_2 blank_5_str_3 blank_5_str_4 blank_5_str_5 blank_5_str_6 blank_5_str_7 blank_5_str_8 blank_5_str_9
Note - the numbers may not be 'surrounded' by '_' characters.
For this data I would like the ‘text’ part data to be considered as blank_5_str_ and the number part of the data to be the number after the last _.
Therefore the first 3 items of the sorted list will be:
blank_5_str_1 blank_5_str_2 blank_5_str_3
Can the sort be changed to allow for this case?
Numbers only
One solution for this is to: 1. Split the data into 2 lists – one for text/number data and one for just numbers 2. Sort both lists independantly 3. Join the list so that the numbers are at the start or end
Is there a better solution than this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting text-number values
by haukex (Archbishop) on Nov 29, 2016 at 13:53 UTC | |
|
Re: Sorting text-number values
by johngg (Canon) on Nov 29, 2016 at 15:19 UTC | |
by merrymonk (Hermit) on Nov 30, 2016 at 09:22 UTC | |
by Corion (Patriarch) on Nov 30, 2016 at 09:25 UTC | |
by merrymonk (Hermit) on Nov 30, 2016 at 10:12 UTC | |
by Corion (Patriarch) on Nov 30, 2016 at 10:16 UTC | |
| |
|
Re: Sorting text-number values
by LanX (Saint) on Nov 29, 2016 at 13:56 UTC | |
|
Re: Sorting text-number values
by poj (Abbot) on Nov 29, 2016 at 14:23 UTC | |
|
Re: Sorting text-number values
by tybalt89 (Monsignor) on Nov 29, 2016 at 16:26 UTC | |
by merrymonk (Hermit) on Nov 30, 2016 at 08:36 UTC | |
|
Re: Sorting text-number values
by tybalt89 (Monsignor) on Nov 30, 2016 at 16:20 UTC |