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

print $query->table( $query->Tr( [ $query->td({width=>"15%"}, "1"), $query->td("2") ] ));
The code above prints 1 and 2 on different lines - because it is putting tr aswell as td tags around each of the two numbers. Why is this? - shouldn't it be printing both on the same line.

Replies are listed 'Best First'.
Re: Tables with Cgi.pm
by Kanji (Parson) on May 11, 2002 at 19:39 UTC

    If you supply an arrayref to CGI's tr method, it will take each element inside that arrayref to be a distinct row, so you need to remove the [ and ]...

    print $query->table( $query->Tr( $query->td({width=>"15%"}, "1"), $query->td( "2"), ), );

    The reason for this is so that you can build rows iteratively, and later pass them to tr and have it (ymmv) Do The Right Thing(tm)...

    my @rows; foreach my $no (qw( one two three )) { push @rows, $query->td( $no ); } print $query->table( $query->Tr( \@rows ) );

        --k.


      Or, replace the comma in the anonymous array with a concentation operattor '.' . That way, both td()s will comprise one "element" in the array, thus, one table row with two columns. (Here I go learning something as I post again - imagine that.)
Re: Tables with Cgi.pm
by giulienk (Curate) on May 11, 2002 at 20:42 UTC
    I guess what you wanted to do was
    print $query->table( $query->Tr( $query->td([1, 2]) ) );
    Still this way you cannot specify {width => "15%"} for one of the cells. So the best way to do it is the one Kanji suggest above.


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Re: Tables with Cgi.pm
by kal (Hermit) on May 11, 2002 at 19:35 UTC
    You want to replace your square brackets with parentheses - ( and ). I'm sure someone here has a very good link to the difference between an anonymous array and a list, but that's essentially the problem.