http://qs1969.pair.com?node_id=503800


in reply to Creating a table using cgi.pm - a unique problem

Y'know, it's really, really nice to post actually parsable example code.

You could just seperate the first td call.

# corrected original my ($value1, $value2, $value3) = (1..3); print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, [ td( { -OnClick=>"javascript:dothis()" }, [ $cgi->a( {-href=>"http://example.com", -target=>"_new"}, $cg +i->img('blah.gif')) . $cgi->a( {-href => 'http://example.com' }, $value1 ). +"\n", $cgi->hidden( -name=>'name2', -value=>"$value2" ). $cgi->a( {-href => 'http://example.com' }, $value2 ) . + "\n", $cgi->hidden( -name=>'name3', -value=>"$value3" ). $cgi->a( {-href => 'http://example.com' }, $value3 ) . + "\n", ]), ]); print "\n"; # td call separated print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, td($cgi->a( {-href=>"http://example.com", -target=>"_new"}, $cgi-> +img('blah.gif')) . $cgi->a( {-href => 'http://example.com' }, $value1 )) +. "\n" . td( { -OnClick=>"javascript:dothis()" }, [ $cgi->hidden( -name=>'name2', -value=>"$value2" ). $cgi->a( {-href => 'http://example.com' }, $value2 ) . + "\n", $cgi->hidden( -name=>'name3', -value=>"$value3" ). $cgi->a( {-href => 'http://example.com' }, $value3 ) . + "\n", ]), );

But since this isn't obviously in a loop, you've got no reason to print your hidden fields within the table; that's just making the code messy. To make things a little saner, store things in advance in a list (for order) and a hash (for name-value association) and built it with a loop.

my @names = (qw(name1 name2 name3)); my %cells = (name1 => {href => 'http://example.com/1', value => $value +1}, name2 => {href => 'http://example.com/2', value => $value +2}, name3 => {href => 'http://example.com/3', value => $value +3}); sub row { my ($names, $cells) = @_; my $first = shift @$names; my @result; push @result, CGI::td(CGI::a({-href=> $cells->{$first}{href}, -targe +t => "_new"}, CGI::img('blah.gif')) . CGI::a({-href=> $cells->{$first}{href}}, $cel +ls->{$first}{value})); for my $name (@$names) { push @result, CGI::td({ -OnClick=>"javascript:dothis()" }, CGI::hidden(-name => '$name', -value => $cel +ls->{$name}{value}) . CGI::a({-href => $cells->{$name}{href}}, $ce +lls->{$name}{value})); } return join '', @result; } print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, row(\@names, \%cells +));

Then chuck that too and start using a template module.