The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Array::Reform - Convert an array into N-sized array of arrays.

SYNOPSIS

  use Array::Reform qw( :all );

  @sample = ( 1 .. 10 );
  $rowsize = 3;

  reform( $rowsize, @sample );
      =>
         (
            [   1,   2,   3   ],
            [   4,   5,   6   ],
            [   7,   8,   9   ],
            [   10   ]
          );

  dissect( $rowsize, @sample );
      =>
         (
            [   1,   5,   9   ],
            [   2,   6,  10   ],
            [   3,   7   ],
            [   4,   8   ]
          );

DESCRIPTION

Both these methods are designed to reformat a list into a list of lists. It is often used for formatting data into HTML tables, amongst other things.

The key difference between the two methods is that dissect() takes elements from the start of the list provided and pushes them onto each of the subarrays sequentially, rather than simply dividing the list into discrete chunks. As a result dissect() returns a list of lists where the first element of each sublist will be one of the first elements of the source list, and the last element will be one of the last. This behaviour is much more useful when the input list is sorted.

Both methods can be called as either functions or class methods (to ensure compatibility with previous releases), and the array to be reformed can be passed as a reference.

AUTHOR

The original code was written by the Perl Monks user lhoward, with contributions by adam, swiftone, and crystalflame.

This version was written by kilinrax, with contributions from davorg.

SEE ALSO

http://perlmonks.org/ --- quick answers to your Perl questions.