Dufonzo has asked for the wisdom of the Perl Monks concerning the following question:

Wet behind the...

This is the first time I've built something like this. This is the work script I wrote, to figure out how to do it. The real script is part of a much larger application.

What I'd like to know is, is there a more elgant way to do this? I don't think this is that bad, but I know there's always a way to use less code. I was also wondering if there is a standard way that a more experienced person would do this.

You can ignore the href's, they're part of the use of this table, long story.

#/usr/bin/perl print "Content-type:text/html\n\n"; print "<html><head><title>Build Table.pl</title></head>\n"; print "<body>\n"; $total_cells = 517; # would be recordset $display_cells = $total_cells / 15; $row_count = 10; # rows for my table $col_count = $display_cells / $row_count; # cols I'll have print "<table cellpadding=\"4\" border=1>\n"; # counters $row_inc = 1; $col_lable = 1; $nav_count = 15; $nav_inc = 15; for($r = 1; $r <= $row_count; $r++) # build rows { print "<tr>\n"; for($c = 0; $c < $col_count; $c++) # build cols { if($nav_count < $total_cells) { print "<td><a href=\"mypage.html?nav=$nav_count\">$col_lab +le</a></td>"; } else { print "<td>&nbsp;</td>"; } $col_lable+=10; $nav_count+=150; } print "</tr>\n"; $row_inc++; $col_lable = $row_inc; $nav_inc+=15; $nav_count = $nav_inc; } print "</table>"; print "</body></html>\n";

Replies are listed 'Best First'.
(jeffa) Re: Dynamic Table
by jeffa (Bishop) on Feb 01, 2003 at 23:12 UTC
    Two things:
    1. use CPAN! This includes using CGI as well as the strict and warnings pragmas.
    2. Insert the items by columns then rows -- not by rows and then columns.
    use strict; use warnings; use CGI::Pretty qw(:standard); my $total_cells = 517; # would be recordset my $display_cells = $total_cells / 15; my $row_count = 10; my $col_count = $display_cells / $row_count; my $col_lable = 1; my $nav_count = 15; my @table; for my $c (0..$col_count) { for my $r (0..$row_count-1) { $table[$r]->[$c] = ($nav_count < $total_cells) ? a{href=>"mypage.html?nav=$nav_count"},$col_lable : '&nbsp;' ; $col_lable += 1; $nav_count += 15; } } print header, start_html('Build Table.pl'), table({cellpadding=>4,border=>1}, map {Tr[td[@$_]]} @table), end_html, ;
    I am sure there is more refactoring that could be done, but this should get you started. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks,

      Very cool, just the kind of input I'm interested in.

      -D

Re: Dynamic Table
by mojotoad (Monsignor) on Feb 02, 2003 at 00:44 UTC
    Just another way to do it using HTML::ElementTable. For CGI applications you will want to stick with the CGI approach noted above, for efficiency. This approach is nice for static generation and tweaking individual cells. Also note you might find HTML::Table of interest.

    #!/usr/bin/perl -w use HTML::ElementTable; use HTML::AsSubs; use POSIX qw(ceil); print "Content-type:text/html\n\n"; print "<html><head><title>Build Table.pl</title></head>\n"; print "<body>\n"; $total_cells = 517; $display_cells = $total_cells / 15; $row_count = 10; $col_count = ceil($display_cells / $row_count); $table = HTML::ElementTable->new(maxrow => $row_count-1, maxcol => $col_count-1); $table->attr('cellpadding' => 4); $table->attr('border' => 1); $table->blank_fill(1); # counters $row_inc = 1; $col_label = 1; $nav_count = 15; $nav_inc = 15; for my $r (0 .. $row_count-1) { for my $c (0 .. $col_count-1) { $table->cell($r,$c)->push_content( a({href => "mypage.html?nav=$nav_count"}, $col_label)) if $nav_count < $total_cells; $col_label+=10; $nav_count+=150; } $row_inc++; $col_label = $row_inc; $nav_inc+=15; $nav_count = $nav_inc; } # Here you could tweak the cells of $table print $table->as_HTML; print "</body></html>\n";

    Have fun,
    Matt