in reply to stack array to array of arrays

This works but it is not cool...

use strict; use warnings; use Data::Dumper; my @array = qw(a b c e 1 2 3 4 5 3 f); my $len = 3; my @stack = map{$_%$len?():[grep{$_}@array[$_..$_+$len-1]]} 0..@array- +1; print Dumper \@stack;

...or this...a destructive version...

@array = map { [ map { shift @array//() } 1..$len ] } 0..int((@array-1)/$len);

Replies are listed 'Best First'.
Re^2: stack array to array of arrays
by spandox (Novice) on Oct 17, 2013 at 13:44 UTC
    the
    grep{$_} .......
    also removes "" and 0 from the list. Should probably be
    grep{defined $_} .....
    Also - why use
    @array -1
    when you could use
    $#array
    I like the approach.... Perhaps
    map{[ grep {defined} @array[$_*$len..$_*$len+$len-1] ]} 0..$#array/$len
    Is a little cleaner?