Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Recursive subroutines with sort

by holli (Monsignor)
on Jul 08, 2019 at 09:40 UTC ( #11102540=perlquestion: print w/replies, xml ) Need Help??

holli has asked for the wisdom of the Perl Monks concerning the following question:

From perldelta:
You can now use recursive subroutines with sort(), thanks to Robin Houston.
No need to follow the link though, that's all it says. Could not find much more. Can somebody enlighten me what this is about?


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re: Recursive subroutines with sort
by hippo (Canon) on Jul 08, 2019 at 10:00 UTC

    sort can take a subroutine name as its first argument. The subroutine performs the comparison and returns the negative, positive or zero result. The delta you've linked to says that as of 5.10.0 such subroutines may now be recursive.

      I've investigated the deltas because of Syntax::Construct and it seemed it had been possible to use recursive subroutines in older Perls, too. 5.10 was just the version that documented the fact.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hah. I didn't know that. I have exclusively used the BLOCK form to this date. It's nice being able to refer to a sub by name, but when is it useful that sub being able to recurse?


      holli

      You can lead your users to water, but alas, you cannot drown them.
        > but when is it useful that sub being able to recurse?

        what comes to mind are chained comparisons in sort

        sort { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] # * or $a->[2] cmp $b->[2] # * or ... # * } @AoA # *) only check if former was equal == 0 == false

        if you don't know the number of fields, recursive comparisons come in handy.

        I'm bu-la-zy, so constructing the recursion is left as exercise. ;-)

        HTH!

        update

        In hindsight ... this particular case could also be solved in a loop.

        So think about a more complicated data structures like trees to be sorted.

        In the end it's about convenience ... every loop can be expressed as recursion and vice versa.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        ) see examples in code from line 39 onwards

Re: Recursive subroutines with sort
by LanX (Archbishop) on Jul 08, 2019 at 16:54 UTC
    > Can somebody enlighten me what this is about?

    Just a guess ... This might be related to the "newish" feature of having a special variable holding the reference of the current sub to facilitate recursions.

    Unfortunately I wasn't able to find it again in order to test if it has issues with older sort implementations.

    update

    __SUB__ it was.

    not a variable though ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      IIRC, __SUB__ was introduced in 5.12 5.16, so it's not likely relevant.

        Perl 5.16

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11102540]
Approved by Eily
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2019-07-19 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?