This example uses Perl::CGI

#!/usr/bin/perl -w use warnings; use strict; package main; # # I really like Perl::CGI -- so this example incorporates it. # use CGI; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; my $human_legible = "\n"; print $q->header, $q->start_html('Table Example'), $q->h1('How to Format a table'); # # Add breaks so ppl can read the HTML output. # This is an example after all. # print $human_legible; # # This is where we figure out spacing. It could be made into a # function easily enough. # my $x_dim = 4; my $y_dim = 5; my $test_text = "This is a test"; my @array = ( $test_text, $test_text, $test_text, $test_text, $test_text, $test_text, $test_text ); my $table_rows = int((scalar @array) / $x_dim); my $last_row_count = (scalar @array) % $x_dim; # # Print the table here. # print $q->start_table; my $array_index = 0; my $table_entries = ""; for ( my $r = 0; $r < $table_rows; $r++ ) { $table_entries = ""; for ( my $j = 0; $j < $x_dim ; $j++ ) { $table_entries .= $q->td($array[$array_index]); $array_index++; } print $q->Tr($table_entries); print $human_legible; } $table_entries = ""; for ( my $i = 0; $i < $last_row_count ; $i++ ) { $table_entries .= $q->td($array[$array_index]); $array_index++; } print $q->Tr($table_entries); print $human_legible; print $q->end_table; print $q->end_html;
The output is:

C:\Code>perl cgitable.pl Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Table Example</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <h1>How to Format a table</h1> <table><tr><td>This is a test</td><td>This is a test</td><td>This is a + test</td> <td>This is a test</td></tr> <tr><td>This is a test</td><td>This is a test</td><td>This is a test</ +td></tr> </table> </body> </html>

In reply to Re: printing out a table X wide and X down without html::template by dwm042
in thread printing out a table X wide and X down without html::template by Anonymous Monk

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.