Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How can I sort my array numerically on part of the string?

by jdporter (Paladin)
on Dec 01, 2020 at 18:09 UTC ( [id://11124465]=note: print w/replies, xml ) Need Help??


in reply to How can I sort my array numerically on part of the string?

You're in luck — the numeric part is at the beginning of the string.

my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my @sorted = sort { $a <=> $b } @list;
I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re^2: How can I sort my array numerically on part of the string?
by haukex (Archbishop) on Dec 01, 2020 at 19:49 UTC
    the numeric part is at the beginning of the string

    True, though I think it's noteworthy this requires a no warnings 'numeric' under warnings, e.g. my @sorted = sort { no warnings 'numeric'; $a <=> $b } @list;.

      now I get a different error "no" not allowed in expression
        now I get a different error "no" not allowed in expression

        Then you probably didn't copy the code correctly.
        Please provide a copy 'n' paste of the script that's producing that error.

        Cheers,
        Rob

        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:  <%-{-{-{-<

Re^2: How can I sort my array numerically on part of the string?
by misterperl (Pilgrim) on Dec 01, 2020 at 20:57 UTC
    I get 1,cat is not numeric. HUH?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found