I've used the splice solution, but it offended me that you have to copy the array just to destroy it. I came up with a version of my mapn function that avoids the splice (and therefore the need to copy as well), by using slices and a pointer.

There are two versions of the function mapn and mapnz. With the former, number of values produced in the last row is just how ever many are left over.

With the latter, it pads (with nulls, hence the 'z') the last list of returned values to be the same size as the rest of the rows. This can be useful for things like tables, where although most browsers will tolorate there being less <td></td> pairs in the last row, they do not render this very well. By using mapnz in conjunction with an 8-char patch I made to CGI.pm so that empty TD's have a &nbsp; inserted, forces most browsers to render the borders around missing values.

#! perl -slw use strict; use CGI qw[td Tr :table caption]; sub mapn (&@) { my($c, $n, $s) = (shift, shift, 0); map{ $s+=$n; $c->( @_[($s-$n) .. ($s < @_ ? $s-1 : @_-1) ] ); } 0 .. (@_/$n); } sub mapnz (&@) { my($c, $n, $s) = (shift, shift, 0); map{ $s+=$n; $c->( @_[($s-$n) .. ($s - 1) ] ); } 0 .. (@_/$n); } my @tabular_data = map{ 1000+int rand 8999 } 1 .. 31; print table( {border=>1}, $/, mapn{ Tr( td( \@_ ) ).$/; } 5, @tabular_data ); print table( {border=>1}, $/, caption('Columns: ' . $_), $/, mapnz{ Tr( td( \@_ ) ).$/ } $_, @tabular_data ) for 3 .. 8; __END__ C:\test>236799 <table border="1"> <tr><td>4978</td> <td>7782</td> <td>3230</td> <td>3524</td> <td>7570< +/td></tr> <tr><td>9627</td> <td>4424</td> <td>7968</td> <td>2523</td> <td>1838< +/td></tr> <tr><td>9843</td> <td>8797</td> <td>5034</td> <td>8499</td> <td>3683< +/td></tr> <tr><td>5591</td> <td>2402</td> <td>6131</td> <td>2649</td> <td>5917< +/td></tr> <tr><td>1333</td> <td>7874</td> <td>7031</td> <td>2269</td> <td>3766< +/td></tr> <tr><td>2151</td> <td>1350</td> <td>7235</td> <td>5986</td> <td>1447< +/td></tr> <tr><td>2627</td></tr> </table> <table border="1"> <caption>Columns: 3</caption> <tr><td>4978</td> <td>7782</td> <td>3230</td></tr> <tr><td>3524</td> <td>7570</td> <td>9627</td></tr> <tr><td>4424</td> <td>7968</td> <td>2523</td></tr> <tr><td>1838</td> <td>9843</td> <td>8797</td></tr> <tr><td>5034</td> <td>8499</td> <td>3683</td></tr> <tr><td>5591</td> <td>2402</td> <td>6131</td></tr> <tr><td>2649</td> <td>5917</td> <td>1333</td></tr> <tr><td>7874</td> <td>7031</td> <td>2269</td></tr> <tr><td>3766</td> <td>2151</td> <td>1350</td></tr> <tr><td>7235</td> <td>5986</td> <td>1447</td></tr> <tr><td>2627</td> <td>&nbsp;</td> <td>&nbsp;</td></tr> </table>

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clerk.

In reply to Re: Fold a list using map splices by BrowserUk
in thread Fold a list using map splices by hiseldl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.