This function is passed a reference to an array and an integer. It reformats the array into an array of arrays where length of each array (except perhaps the last one - for obvious reasons) is equal to the given integer.
sub reform {
my ($r_old, $x) = @_;
my @old = @{$r_old};
my @new;
push @new, [splice @old, 0, $x] while @old;
@new;
}