This is a way you could do it using
Template Toolkit. Using a template system allows you to keep the HTML out of your code and will also handle writing the new files for you:
use Template;
my $template = Template->new;
my $tmpl = 'table.tmpl';
my %names;
while ( my $line = <DATA> ) {
chomp $line;
my ($name,$item,$answer) = ($line =~ m/^(\w+)\s+(\w+)\s*-\s*(.*)/)
+;
push( @{ $names{$name} }, { item=>$item, answer=>$answer } );
}
for my $name ( keys %names ) {
my $table = { title=>$name, rows=>$names{$name} };
$template->process( $tmpl, $table, "$name.html" )
|| die $template->error();
}
__DATA__
Filename1 Item1 - Answer
Filename1 Item2 - Answer
Filename2 Item1 - Answer
Filename2 Item2 - Answer
This will create two new files "Filename1.html" and "Filename2.html".
The content part of the template file "table.tmpl" would look like this:
<h1>[% title %]</h1>
<table>
[% FOREACH row IN rows %]
<tr>
<td>[% row.item %]</td>
<td>[% row.answer %]</td>
</tr>
[% END %]
</table>
Obviously, you need to add the html and body tags around this, and you can also add CSS and other static elements.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link or
or How to display code and escape characters
are good places to start.