1

I have a Perl CGI script. I would like to make a dynamic, appropriately-sized table based on query information from a simple HTML form: http://jsfiddle.net/wBgBZ/4/. I wanted to use HTML::Table but the server doesn't have the module installed. The administrator won't install it either. Therefore, I have to do it the old fashion way.

Here's what I have so far.

#!/usr/bin/perl
use strict; use warnings;
use CGI qw( :standard);

print header;
print start_html(
    -title => 'Creating Tables'
);

# Process an HTTP request
my $query = param("names");
my @students_in_class = split(/;/, $query);
my %attributes = (
    'Tommy'   => 'A star baseball player who has lots of potential to play in the Major League of Baseball. ',
    'Tyrone'  => 'An honor roll athlete. His father is really proud of him. When he graduates, he wents to work at the National Institute for Public Health. His father wants him to become a doctor but he wants to pursue Physics.',
    'Marshall' => 'A professional WWE wrestler.',
);

print table({-border=> undef},
    caption('Students in the class'),
        Tr({-align=>'CENTER',-valign=>'TOP'},
            [ 
             th(['Student', 'List of Attributes']),
             foreach (@students_in_class){       # !!!!! problem line !!!!!!
                 td(['$_' , '$attributes{$}']),
             }
             ]
           )
 );

Such that if the user enters the following into the search bar: Tyrone;Tommy;Marshall

the CGI should produces something similar to the following

Desired Output

http://jsfiddle.net/PrLvU/


If the user enters just Marshall;Tommy, the table should be 3x2.

It doesn't work. I need a way to dynamically add rows to the table.

2
  • What do you mean exactly by 3x2? Commented May 11, 2013 at 0:37
  • @JasonGray 3 rows, 2 columns. The header Student | List of Attributes is considered a row. Commented May 11, 2013 at 0:40

1 Answer 1

1

This is untested, but I think this is what you are wanting. You may need to change some of the table attributes to your desired needs.

use strict; 
use warnings;
use CGI qw( :standard );

print header,
      start_html(-title => 'Creating Tables');

my $query = param('names');

my @headers;
my @students = split(/;/, $query);

my %attributes = (
        Tommy   => 'A star baseball player.',
        Tyrone  => 'An honor roll athlete.',
       Marshall => 'A professional WWE wrestler.',
);

$headers[0] = Tr(th('Student'), th('List of Attributes'));

for my $i (@students) {
  push @headers, Tr( td($i), td($attributes{$i}));
}

print table( {-border => undef}, @headers );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.