Q:

How can I divide a Perl array into smaller arrays?

Is there any Perl equivalent for php's array_chunk()?
I'm trying to divide a large array into several smaller ones.
Thanks in advance.

A:

splice() function.

A:

You can use array slices like this:
#!/bin/perl -l

my @array = (1,2,3,4,5,6,7,8,9);

print join ",", @array[0..2];
print join ",", @array[3..5];
print join ",", @array[6..$#array];

A:

To match the PHP function you can use a number of approaches:
use strict;
use warnings;

use List::MoreUtils qw( natatime part );

use Data::Dumper;

my @array = 0..9;
my $size = 3;

{    my $i = 0;
     my @part = part { int( $i++ / $size ) } @array;
     warn "PART\n";
     warn Dumper \@part;
}

{    my $iter = natatime( $size, @array );
     my @natatime;
     while( my @chunk = $iter->() ) {
        push @natatime, \@chunk;
     }
     warn "NATATIME\n";
     warn Dumper \@natatime;
}

{    my @manual;
     my $i = 0;

     for( 0..$#array ) {
         my $row = int( $_ / $size );

         $manual[$row] = [] 
             unless exists $manual[$row];

         push @{$manual[$row]}, $array[$_];
     }

     warn "MANUAL:\n";
     warn Dumper \@manual;
}

