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

Hi Monkees

How would I generate a series of TD values from an array using 'proper' CGI syntax?

e.g. (make this work - simplified in this example to a single column)

@filesout = qw(a b c); print table({-border=>1},Tr([ th(['Document']), foreach(@filesout){ td([a({-href=>$_},$_)]) } ]) );
Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: CGI table rows from an array
by davidrw (Prior) on Oct 10, 2006 at 13:20 UTC
    I think map is what you're looking for instead of the foreach in there:
    my @rows = ( [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], ); print table({-border=>1},Tr([ th(['Document', 'X', 'Y', 'Z']), map { td([ map { a({-href=>$_},$_) } @$_ ]) } @rows ]) );

      Indeed it is - many thanks. Never used map before.

      ...and the final code:

      print table({-border=>1}, Tr([ th(['Document', 'On TWiki']), map{ td([ a({-href=>$$_[2]},$$_[3]), a({-href=>$$_[1]},$$_[0]) ]) } @filesout ]) );
      Tom Melly, tom@tomandlu.co.uk
Re: CGI table rows from an array
by odha57 (Monk) on Oct 10, 2006 at 13:56 UTC
    I have to run off to work in the lab, but here is some snippets from some cgi stuff I use. It is from a book by Lincoln Stein called "Official Guide to Programing with CGI.pm " and has been a handy book. In this example, it goes through some hashes and creates a first entry of a link, then puts other values from the hashes into a list. then the list with a td header is pushed onto a list called @rows. The last line of the table creates the table rows including the column header. I tried looking for examples in perldoc CGI but didn't find it in there.
    #!/usr/bin/perl use CGI ':standard',':html3'; foreach $key(sort(keys %ph_isv)){ $key_link = '<a href=../cgi-bin/partgraph?part='.$key.'>'.$key.'</ +a>'; @data = ($key_link,$ph_isv{$key},$ph_oos{$key},$aa_inprg{$key},$aa +_hand{$key},$aa_redir{$key},); push @rows,td(\@data); } @col_head = ('Partition', 'In Service','Out of Service','In Progress', + 'Handled', 'Redirected'); print table({-border=>'', -align=>CENTER}, caption(strong($caption)), TR(td({-colspan=>1},[' ']),td({-colspan=>2, -align=>CENTER},[ +'<b>Phones</b>']),td({-colspan=>3, -align=>CENTER},[ '<b>Auto Attenda +nt</b>'])), TR([th(\@col_head),@rows]) ) ;