Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to ignore and retrieve certain values from text file which is to be split into multiple HTML files with tables?

by tangent (Vicar)
on Apr 23, 2016 at 10:45 UTC ( #1161302=note: print w/replies, xml ) Need Help??


in reply to How to ignore and retrieve certain values from text file which is to be split into multiple HTML files with tables?

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.
  • Comment on Re: How to ignore and retrieve certain values from text file which is to be split into multiple HTML files with tables?
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-22 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?