Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: How can I do a numeric sort on a substring?

by misterperl (Monk)
on Jun 25, 2021 at 14:01 UTC ( #11134275=note: print w/replies, xml ) Need Help??


in reply to Re: How can I do a numeric sort on a substring?
in thread How can I do a numeric sort on a substring?

TYVM that's very helpful. Is the [0] there in case there are more than one number matching like a-1-3 ? TY!
  • Comment on Re^2: How can I do a numeric sort on a substring?

Replies are listed 'Best First'.
Re^3: How can I do a numeric sort on a substring?
by haukex (Bishop) on Jun 25, 2021 at 14:07 UTC
      Thanks for pointing to Sort::Key , seems it covers all use cases of the Schwartzian transform with a short syntax. :)

      The documentation could be simpler tho, by deconstructing the naming convention.

      [|r][|n|i|u]keysort[_inplace] @array

      (Like this the doc seem even to be not logical/ wrong in one case)

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

      sort key natural IS DA BOMB!! TY TY this made my week :)
      I never saw that Natural sort- you are on fire today all ++ votes! TYVM Have a great weekend- I'm headed off to study your suggestions..
        For an even more general sort, see the function 'sort_by' in List::UtilsBy.
        Bill
Re^3: How can I do a numeric sort on a substring?
by hippo (Bishop) on Jun 25, 2021 at 15:12 UTC

    Not specifically. The regex match is context sensitive, so the brackets around it enforce list context and then the [0] pulls out the first value of that list before the <=> gets a chance to enforce scalar context on it. Example:

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $x = 'a-9'; say "Scalar: " . $x =~ /(\d+)/; say "List: " . ($x =~ /(\d+)/); say "First: " . ($x =~ /(\d+)/)[0];

    Read lots more in the Context tutorial.


    🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2021-10-21 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (83 votes). Check out past polls.

    Notices?