Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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,



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985940]
[huck]: my @aa; for my $key (sort keys %hash){push @aa,$key,$hash{$ke y};} print join(',',@aa)."\n ";

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-05-27 08:05 GMT
Find Nodes?
    Voting Booth?