CSV to HTML translation: Difference between revisions

Added Perl 5.
(→‎{{header|Python}}: Add link to show rendered outputs.)
(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>&lt;angry&gt;Now you listen here! He&#39;s not the messiah; he&#39;s a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
845

edits