|Welcome to the Monastery|
Rosetta code: Split an array into chunksby eyepopslikeamosquito (Chancellor)
|on Sep 25, 2010 at 09:29 UTC||Need Help??|
Following the educational tradition of:
Given a list of strings, for example ("a", "bb", "c", "d", "e", "f", "g", "h"), and a chunksize, for example 3, write a subroutine to return a multi-line string, for example:
The output string must contain a single space between each array element and a newline every chunksize items. Note that no trailing space is permitted on any line and the last line must be properly newline-terminated.
Here was my first Perl attempt:
I trust this initial solution will clarify the problem specification.
Being dissatisfied with this ugly first attempt, I next took at a look at the core List::Util and the non-core List::MoreUtils modules, writing two different solutions using List::MoreUtils, one using natatime, the other part, as shown below:
For cheap thrills, I hacked out a Python itertools-based solution.
I've derived little enjoyment so far from any of my solutions and accordingly encourage you to show us a more elegant way to solve this simple problem.
Please feel free to contribute more Perl solutions or a solution in any language you fancy. I'm especially eager to admire a Perl 6 solution.