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 (Archbishop) 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 (Archbishop) 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.


    🦛