<?xml version="1.0" encoding="windows-1252"?>
<node id="985940" title="Re: How to return a list using recursion?" created="2012-08-07 07:06:42" updated="2012-08-07 07:06:42">
<type id="11">
note</type>
<author id="401112">
johngg</author>
<data>
<field name="doctext">
&lt;p&gt;Another recursive solution using a subroutine that takes a block of code to execute against a "chunked" list.&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

use 5.010;

sub groupsOf (&amp;$@);

my @list = ( q{a} .. q{z} );

say for groupsOf { qq{@_,} } 5, @list;
say for groupsOf { qq{@_,} } 7, @list;

sub groupsOf (&amp;$@)
{
    my $rcToRun  = shift;
    my $groupsOf = shift;

    my $rcDoIt;
    $rcDoIt = sub
       {
           $rcToRun-&gt;(
              map shift, 1 .. ( @_ &lt; $groupsOf ? @_ : $groupsOf )
              ),
           @_ ? &amp;$rcDoIt : ();
       };

    &amp;$rcDoIt;
}
&lt;/code&gt;
&lt;p&gt;I hope this is of interest.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update: &lt;/b&gt;Oops, forgot to post the output :-(&lt;/p&gt;
&lt;code&gt;
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,
&lt;/code&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-401112"&gt;
&lt;p&gt;Cheers,&lt;/p&gt;&lt;p&gt;JohnGG&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
985857</field>
<field name="parent_node">
985857</field>
</data>
</node>
