Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 lurking in the Monastery: (6)
As of 2014-08-20 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (124 votes), past polls