Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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,



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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2015-12-01 23:25 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (29 votes), past polls