Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
you want Tr () to output your hash. so, each key/value pair inside the hash must be encapsulated inside a td () - the loop to use therefore is "map", not "for".

for is only used for the sideeffects of the corresponding block that is applied to each of the elements of the list. especially the for does not return anything meaningful.

the map takes a list, and returns a list - and that is what you want. you specify the transform of each element in the list in the map block. consider this:

use strict; use warnings; use CGI qw/:standard/; my %hosts = ('a' .. 'z'); cgiOut (); sub cgiOut { my $q = new CGI; print $q->header( "text/plain" ), $q->start_html( -title=>"Apache Stats for \$hostname", -bgcolor=>"#ffffff"), $q->h2( "Apache Stats for \$hostname" ), $q->hr, $q->table( { -border=>"1", -width=>"100%" }, $q->Tr({}, [map { td([$_, $hosts{$_}]) } sort keys %hosts])), $q->end_html; }

HTH

In reply to Re: Constructing HTML tables with CGI.pm by kabel
in thread Constructing HTML tables with CGI.pm by lacertus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-03-19 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found