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 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> </td> <td> </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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.