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
%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?


In reply to Sorting text-number values by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.