my @a=(0..12); # Admittedly, not the best choice for variable names... my @b; for (my $i=0; $i <= $#a; $i+=5) { push @b,join(',', @a[$i..$i+4]); print qq[ $i:$a[$i]=@b \n]; } # @b ends up with comma-separated, 5-element (or less for last chunk) chunks of @a