Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
There's more than one way to do things
 
PerlMonks  

Re: How to return a list using recursion?

by Athanasius (Prior)
on Aug 07, 2012 at 02:38 UTC ( #985864=note: print w/ replies, xml ) Need Help??


in reply to How to return a list using recursion?

Use array references:

#! perl use strict; use warnings; my @list = ( 'a' .. 'z' ); my @array_chunk5 = _array_chunk5( @list ); print "Array chunk of 5 =\n"; my $count; print "Chunk ", ++$count, ":\n", join("\n", @$_), "\n\n" for @array_ch +unk5; sub _array_chunk5 { my @arr = @_; if (@arr > 5) { my @chunk; push(@chunk, shift @arr) for (1 .. 5); return ( \@chunk, _array_chunk5(@arr) ); } else { return \@arr; } }

HTH,

Athanasius <°(((><contra mundum


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-04-23 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (552 votes), past polls