Perl Monk, Perl Meditation | |
PerlMonks |
Sorting text-number valuesby merrymonk (Hermit) |
on Nov 29, 2016 at 13:44 UTC ( [id://1176809]=perlquestion: print w/replies, xml ) | Need Help?? |
merrymonk has asked for the wisdom of the Perl Monks concerning the following question:
A recent question of mine asked how to sort data, which was <alpha character><numeric characters> for example <Ab1234>.
Several Monks kindly gave suggestions and the ‘heart’ of one of them was
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
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:
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?
Back to
Seekers of Perl Wisdom
|
|