What version of Perl are you using?

The
    sort { no warnings 'numeric';  $a <=> $b; }
trick works for your particular data under Perl version 5.30.3, but does not work under version 5.8.9.

Win8 Strawberry 5.30.3.1 (64) Wed 12/02/2020 16:16:13 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my @sorted = sort { no warnings 'numeric'; $a <=> $b } @list; print for @sorted; ^Z 1,cat 001,elk 2,dog 11,eel 13,mink 22,mouse Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 16:18:25 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); # my @sorted = sort { no warnings 'numeric'; $a <=> $b } @list; my @sorted = sort { no warnings; $a <=> $b; } @list; "no" not allowed in expression at - line 3, at end of line BEGIN not safe after errors--compilation aborted at - line 3.
I haven't done the work to figure out the version number at which no starts to work in a sort subroutine block.

Interestingly (?), no is supported in a sub subroutine block.

Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 16:19:55 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l my @list = ('1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,min +k'); my @sorted = sort numeric_ascending @list; print for @sorted; sub numeric_ascending { no warnings 'numeric'; $a <=> $b; } ^Z 1,cat 001,elk 2,dog 11,eel 13,mink 22,mouse

Update: ... and also works in a do { ... }; block:

Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 18:17:07 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l my $sum = do { '98xyzzy' + '763foo'; }; print $sum; $sum = do { no warnings 'numeric'; '123abc' + '45defg'; }; print $sum; ^Z Argument "763foo" isn't numeric in addition (+) at - line 2. Argument "98xyzzy" isn't numeric in addition (+) at - line 2. 861 168
(Same results under version 5.30.3.)


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: How can I sort my array numerically on part of the string? (updated) by AnomalousMonk
in thread How can I sort my array numerically on part of the string? by misterperl

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.