Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: How to return a list using recursion?

by Athanasius (Monsignor)
on Aug 07, 2012 at 04:26 UTC ( #985874=note: print w/ replies, xml ) Need Help??


in reply to Re: How to return a list using recursion?
in thread How to return a list using recursion?

this isn't something you should use recursion for

s/should/need to/

You also don’t need a module. Here is a solution using a stateful subroutine as an iterator:

#! perl use strict; use warnings; my @list = ('a' .. 'z'); my $size = 5; _array_chunk_it($size, @list); while (my @chunk = _array_chunk_it()) { print "Array chunk of $size =\n", join("\n", @chunk), "\n\n"; } { my ($array, $count); sub _array_chunk_it { my @chunk; if (@_) { $count = shift; $array = [ @_ ]; } else { for (1 .. $count) { push(@chunk, shift @$array) if @$array; } } return @chunk; } }

(With newer versions of Perl, this can be rewritten using state.)

TMTOWTDI :-)

Update: ++Anonymous Monk for the superior version using a closure, below.

Athanasius <°(((><contra mundum


Comment on Re^2: How to return a list using recursion?
Download Code
Re^3: How to return a list using recursion?
by Anonymous Monk on Aug 07, 2012 at 15:04 UTC

    With older versions of Perl, it can be rewritten as a proper iterator :-)

    use strict; use warnings; my @list = ('a' .. 'z'); my $size = 5; my $chunker = _array_chunk_it($size, @list); while (my @chunk = $chunker->()) { print "Array chunk of $size =\n", join("\n", @chunk), "\n\n"; } sub _array_chunk_it { my ($count, @array) = @_; return sub { my @chunk; for (1 .. $count) { push(@chunk, shift @array) if @array; } return @chunk; } }

    (Using a closure we can actually call _array_chunk_it multiple times, in an overlapping manner. Have multiple iterators running simultaneously. Plus it looks quite a bit nicer.)

Re^3: How to return a list using recursion?
by Anonymous Monk on Aug 08, 2012 at 08:32 UTC

    this isn't something you should use recursion for

    s/should/need to/

    :) I meant should not, esp since there is nothing particularly recursive about it (you have to squint to make it recursive)

    You also donít need a module.

    Sure I do :) otherwise I'm writing the the idiomatic perl solution

    It was real hard to come up with

    #!/usr/bin/perl -- use strict; use warnings; my @x = ('a' .. 'z'); print yup( 5, @x ),"\n--\n"; print yup( 3, @x ),"\n"; sub yup { my( $n, @x ) = @_; my @yo; for( my $ix = 0; $ix < @x; $ix += $n ) { my $high = $n - 1 + $ix ; $high > $#x and $high = $#x ; push @yo, join ' ', @x[ $ix .. $high ]; } return join ",\n", @yo; } __END__ 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-22 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (178 votes), past polls