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

Re: How to return a list using recursion?

by Athanasius (Abbot)
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 drinking their drinks and smoking their pipes about the Monastery: (21)
As of 2015-07-01 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (6 votes), past polls