in reply to table()'s with CGI.pm

One of the nice things about the CGI.pm table and other params is that if you pass, say, TD, an array value, it will make a TD for each one of those array values, with the optional modifiers for the tag. Combine this with map, and you can easily make complex tables:
print $cgi->table( $cgi->Tr( map { $cgi->td( [ $_, @{$sites{$_}} ] ) } keys %sites ) # Tr ); #table

update: should be keys %sites as davorg pointed out.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: table()'s with CGI.pm
by davorg (Chancellor) on Jun 26, 2001 at 12:03 UTC

    Just a couple of points on that code.

    1. I think you meant keys %sites
    2. I find the object interface to CGI.pm clutters code up unnecessarily and can confuse beginners

    Here's a complete test program based on your code:

    #!/usr/bin/perl -w use strict; use CGI qw (:standard); my %sites; while (<DATA>) { chomp; my($name, @status) = split/\|/; $sites{$name} = [@status]; } print header; print start_html; print table(Tr(th(['Site Name', 'Site Address', 'Status', 'Comments'])), Tr([ map { td([$_, @{$sites{$_}}])} keys %sites ])); print end_html; __END__ site A|www.sitea.com|0|Not ready site B|www.siteb.org|1|Ready!!
    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>