Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by haukex (Archbishop)
on Dec 01, 2020 at 17:10 UTC ( [id://11124463]=note: print w/replies, xml ) Need Help??


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

How about sort { ($a=~/(\d+),/)[0] <=> ($b=~/(\d+),/)[0] or $a cmp $b } @list? (see also return values of regular expressions) Though a Schwartzian transform would be much more performant:

@list = map { $$_[0] } sort { $$a[1] <=> $$b[1] or $$a[0] cmp $$b[0] } map { /(\d+),/; [$_,$1] } @list;

Note I added the or cmp so that if the numeric parts are equal (e.g. '1,cat' vs. '001,elk'), the list is still reliably sorted.

Update: The above doesn't handle cases of the regex not matching. In my second piece of code above you could handle that with an error via e.g. map { /(\d+),/ or die $_; [$_,$1] } or a replacement value via e.g. map { [$_, /(\d+),/ ? $1 : 0] }.

Replies are listed 'Best First'.
Re^2: How can I sort my array numerically on part of the string? (updated)
by LanX (Saint) on Dec 02, 2020 at 02:09 UTC
    >  sort { ($a=~/(\d+),/)[0] <=> ($b=~/(\d+),/)[0]

    You need that (...)[0] for the match to return the captures in list context, ( <=> is enforcing scalar context and m// only returns captures in list context otherwise boolean )

    I'm wondering if there is a prettier solution for that.

    The Schwartzian transform doesn't have that limitation.

    ... map { [$_, /(\d+),/] } @list;

    should do already.

    update

    clarification: any better solution than (...)[0] to get list context ?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Here's a capture in list context. Prettier?

      #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11124461 use warnings; my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my @n; @n[ /(\d+),/ ] .= "$_\n" for @list; print grep defined, @n;

      This is why perl is fun :)

        > This is why perl is fun :)

        and so memory efficient ...

        > Prettier?

        nope! :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      TY I didnt realise I needed the [0] I'm gonna try that. Although I'm unclear as to what "list" is involved with $a and $b which appear to be scalar?

        > > You need that (...)[0] for the match to return the captures in list context, ( <=> is enforcing scalar context and m// only returns captures in list context otherwise boolean )

        DB<96> $_= "123,foo" DB<97> p scalar m/(\d+,)/ # boolean value 1 DB<98> p m/(\d+,)/ # list of captures 123, DB<99>

        see https://perldoc.perl.org/perlop#Matching-in-list-context

        If the /g option is not used, m// in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, that is, ($1, $2, $3...) (Note that here $1 etc. are also set). When there are no parentheses in the pattern, the return value is the list (1) for success. With or without parentheses, an empty list is returned upon failure.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        the list is the return values of the regex

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found