Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by misterperl (Pilgrim)
on Dec 01, 2020 at 21:01 UTC ( [id://11124478]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How can I sort my array numerically on part of the string?
in thread How can I sort my array numerically on part of the string?

now I get a different error "no" not allowed in expression
  • Comment on Re^3: How can I sort my array numerically on part of the string?

Replies are listed 'Best First'.
Re^4: How can I sort my array numerically on part of the string?
by syphilis (Archbishop) on Dec 02, 2020 at 04:14 UTC
    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
Re^4: How can I sort my array numerically on part of the string? (updated)
by AnomalousMonk (Archbishop) on Dec 02, 2020 at 21:49 UTC

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

Log In?
Username:
Password:

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

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

    No recent polls found