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

Replies are listed 'Best First'.
RE: Reformat Array
by merlyn (Sage) on Jun 16, 2000 at 20:19 UTC
    Could be nice to return a reference to @new rather than a copy, as a general rule. The my will ensure that each new invocation gets a separate structure.

    -- Randal L. Schwartz, Perl hacker

RE: Reformat Array
by princepawn (Parson) on Jun 16, 2000 at 21:38 UTC
    You might want to check out
    Array::Reform
    which on CPAN under my author id. It is nothing but a verbatim copy of lhoward's solution
      I've seen Array::Reform. This solution was written when I realised how inefficient Array::Reform was. I was thinking of submitting this solution as a patch to Array::Reform. Dave...
      --
      http://www.dave.org.uk

      European Perl Conference - Sept 22/24 2000
      http://www.yapc.org/Europe/