Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to return a list using recursion?

by johngg (Abbot)
on Aug 07, 2012 at 11:06 UTC ( #985940=note: print w/ replies, xml ) Need Help??


in reply to How to return a list using recursion?

Another recursive solution using a subroutine that takes a block of code to execute against a "chunked" list.

use strict; use warnings; use 5.010; sub groupsOf (&$@); my @list = ( q{a} .. q{z} ); say for groupsOf { qq{@_,} } 5, @list; say for groupsOf { qq{@_,} } 7, @list; sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. ( @_ < $groupsOf ? @_ : $groupsOf ) ), @_ ? &$rcDoIt : (); }; &$rcDoIt; }

I hope this is of interest.

Update: Oops, forgot to post the output :-(

a b c d e, f g h i j, k l m n o, p q r s t, u v w x y, z, a b c d e f g, h i j k l m n, o p q r s t u, v w x y z,

Cheers,

JohnGG


Comment on Re: How to return a list using recursion?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2015-07-04 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls