in reply to Re^6: Fold a list using map splices
in thread Fold a list using map splices

I wonder if its even necessary to have a callback function.

package HTML::Table; use CGI; my $cgi = CGI->new(); sub map_rows (@) { my $n = splice(@_, 0, 1); push(@_, '' x (-@_ % $n)); my @table_rows = map {$cgi->Tr( $cgi->td( [splice @_, 0, $n] ) ) } 0..((@_/$n) - 1); } sub make_table { my ($columns, @elements) = @_; return $cgi->table( map_rows $columns, @elements ); } 1;

And the testing code

use HTML::Table; my $table = HTML::Table::make_table(5, qw[some random data to test th +e table creation thing] ); print $table;

Output

<table><tr><td>some</td> <td>random</td> <td>data</td> <td>to</ td> <td>test</td></tr> <tr><td>the</td> <td>table</td> <td>creation</t +d> <td>thing</td> <td></td></tr></table>

Or perhaps

some random data to test
the table creation thing

Replies are listed 'Best First'.
Re^8: Fold a list using map splices
by Aristotle (Chancellor) on Feb 20, 2006 at 18:42 UTC

    The callback is not necessary; you can translate anything written with map to foreach. I still prefer map for a whole class of tasks, though.

    Makeshifts last the longest.

Re^8: Fold a list using map splices
by Anonymous Monk on Apr 12, 2007 at 13:00 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $SPLITSIZE = 1 + int rand(6); my $len = int rand(20); printf "SPLITSIZE=%d, len=%d\n", $SPLITSIZE, $len; my @list = 1..$len; @list = map[splice @list, 0, $SPLITSIZE], 0..$#list/$SPLITSIZE; print Dumper(\@list);