Ing. Branislav Gerzo wrote:
> > > print table(
> >         {-border=>>undef},
> > >         caption('Choose your favourite brand:'),
> > >         Tr({-align=>CENTER,-valign=>TOP},),
> > >         td($items[0], $items[1], $items[2]),
> > >         td($items[3], $items[4], $items[5])
> > >         );
> 
> I want print all items into table, table should have 3 columns.
> I don't know how to do it.

What you have prints one row with two columns. It looks like perhaps you
want two rows with three columns in each row.

To simplify, here's what you have:

   print Tr(td(0, 1, 2), td(3, 4, 5))

Which emits:

  <tr><td>0 1 2</td> <td>3 4 5</td></tr>

What you should do is pass an array reference to both Tr() and td(), which
causes them to emit separate elements for each entry in the array:

   print Tr([ td([ 0, 1, 2 ]), td([ 3, 4, 5 ]) ])

Which emits:

   <tr><td>0</td> <td>1</td> <td>2</td></tr> 
   <tr><td>3</td> <td>4</td> <td>5</td></tr>

Note how each element is now inside it's own cell.

Now, if the number of entries in @items is variable, you need to print
enough rows of three elements each in order to display the entire array.

Here's a little script to illustrate:

   #!/usr/bin/perl
   use strict;
   use CGI ':standard';
   my @items = map "value $_", 1..10;
   my $ncol = 3;
   print Tr([ map td([ @items[ $ncol * $_ .. $ncol * $_ + $ncol - 1 ] ]),
       0 .. @items / $ncol]);

That last line is a bit complex, so let me break it down:

   print Tr([ ...some rows... ]);

The square brackets indicates an array reference. This causes CGI to output
a <tr> element for each entry in "some stuff". So "some rows" is a list of
rows in the table.

The rows are formed by:

   map ...a row... , 0 .. @items / $ncol

Perl's map() function returns a list, by iterating over the list 0 .. @items
/ $ncol and evaluating "a row" for each value in that list. @items / $ncol
is 10 / 3, which is 3.3333, so the range is 0 .. 3.3333, which perl treats
as 0 .. 3, or 0, 1, 2, 3. These are basically the row numbers in the table.
Since we have ten elements to display in three columns, it will take four
rows to display them (0 .. 3).

The map() call then needs to output a string of HTML representing the
contents of a single row (whose row number is in $_). That's what "a row"
needs to do.

The code to output the HTML for a single row is:

   td([ @items[ $ncol * $_ .. $ncol * $_ + $ncol - 1 ] ])

Again, the outermost set of square brackets supplies an anonymous array to
td(), which causes CGI to output a separate <td> element for each entry in
the array. The array is a slice from @items consisting of the elements for
the row number in $_. If the row number is 0, we want elements 0, 1, and 2.
If the row number is 2, we want elements 6, 7, and 8. The starting element
is always $ncol * $_, and the ending element is ($ncol - 1) beyond that.

If you can get all the maths down right, this kind of thing is extremely
powerful for generating dynamic tables, forms, etc. Take a look at "perldoc
CGI" under the heading "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS"

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to