CSV to HTML translation: Difference between revisions
Added Perl 5.
(→{{header|Python}}: Add link to show rendered outputs.) |
Underscore (talk | contribs) (Added Perl 5.) |
||
Line 249:
</tr>
</tbody>
</table></lang>
=={{header|Perl}}==
Provide the CSV data as standard input. With a command-line argument, the first row will use <code><th></code> instead of <code><td></code>.
<lang perl>use HTML::Entities;
sub row {
my $elem = shift;
my @cells = map {"<$elem>$_</$elem>"} split ',', shift;
print '<tr>', @cells, "</tr>\n";
}
my ($first, @rest) = map
{my $x = $_; chomp $x; encode_entities $x}
<STDIN>;
print "<table>\n";
row @ARGV ? 'th' : 'td', $first;
row 'td', $_ foreach @rest;
print "</table>\n";</lang>
Output (with a command-line argument):
<lang html4strict><table>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
|