Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^7: Fold a list using map splices

by hypochrismutreefuzz (Scribe)
on Feb 20, 2006 at 14:02 UTC ( #531409=note: print w/replies, xml ) Need Help??


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);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://531409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2023-12-03 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (20 votes). Check out past polls.

    Notices?