How can I divide a Perl array into smaller arrays?


Question

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.

1
3
1/14/2010 6:52:12 PM

Accepted Answer

splice() function.

14
1/14/2010 3:21:17 PM

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];

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon