Jaron787 Jaron787 - 5 months ago 16
HTML Question

Perl Print regex matches from an array to a html <td>

I have the following code in perl which is outputting each value of an array to a new HTML table cell and table row

print "<tr><td>$_<br/></td></tr>\n" for @fruitArray;


The contents of the array are as follows:

APPLE|0
ORANGE|0
PEAR|1
GRAPE|0
TOMATO|1


I want to print everything before the | in the first table cell and then everything after the | in a second table cell on that row

I presume I need to use regex something like:

/([|])\w+/


Just unsure on the exact syntax of printing this in one line

Answer

This is really very simple

use strict;
use warnings 'all';

my @data = qw/
    APPLE|0
    ORANGE|0
    PEAR|1
    GRAPE|0
    TOMATO|1
/;

print "<table>\n";
printf "    <tr><td>%s</td><td>%s</td></tr>\n", split /\|/ for @data;
print "</table>\n";

output

<table>
    <tr><td>APPLE</td><td>0</td></tr>
    <tr><td>ORANGE</td><td>0</td></tr>
    <tr><td>PEAR</td><td>1</td></tr>
    <tr><td>GRAPE</td><td>0</td></tr>
    <tr><td>TOMATO</td><td>1</td></tr>
</table>