in reply to Re: printing out a table X wide and X down without html::template
in thread printing out a table X wide and X down without html::template

I take a inside-out approach and use CGI's distributive magic for building the td and tr blocks.
#!/usr/bin/perl use warnings; use strict; use CGI; use constant COLS => 4; use constant ROWS => 5; use constant TABLE_SIZE => COLS * ROWS; my $cgi = new CGI; # Loop through the pages, to pretend we got a page number from a reque +st... foreach my $page_num ( 1 .. 6 ) { # Create a dummy list of image names. We're adding an orphan to test + a short # table display. my @image_names = map {"image_$_.jpg"} 1 .. 101; # ---------------------------------------- # this does the real work... # ---------------------------------------- # Get a page's table's worth of image names and wrap them inside < +img> # tags... my @imgs = map { $cgi->img($_) } splice( @image_names, ( $page_num - 1 ) * TABLE_SIZE, TABLE_SI +ZE ); # Make sure the table has enough elements. Add blank elements if not + to keep # the rows balanced... if ( scalar(@imgs) % COLS ) { push @imgs, ('&nbsp;') x ( COLS - ( scalar(@imgs) % COLS ) ); } # Read "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS" in the CGI pe +rldoc. # # Process rows in column-sized chunks... my @rows = (); while (@imgs) { push @rows, join( '', $cgi->td( [ splice( @imgs, 0, COLS ) ] ) + ); } print $cgi->table( $cgi->Tr( \@rows ) ), "\n\n"; # ---------------------------------------- # end o' working part # ---------------------------------------- }
  • Comment on Re^2: printing out a table X wide and X down without html::template
  • Download Code