Reputation: 560
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
Upvotes: 2
Views: 125
Reputation: 126742
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";
<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>
Upvotes: 7
Reputation: 98961
You may need something like:
#!/usr/bin/perl
use strict;
use warnings;
my @names = ("APPLE|0", "ORANGE|0", "PEAR|1", "GRAPE|0", "TOMATO|1");
foreach my $n (@names) {
while ($n =~ m/(.*?)\|(\d+)/g) {
print "<tr><td>$1<br/></td></tr>\n";
print "<tr><td>$2<br/></td></tr>\n";
}
}
Output:
<tr><td>APPLE<br/></td></tr>
<tr><td>0<br/></td></tr>
<tr><td>ORANGE<br/></td></tr>
<tr><td>0<br/></td></tr>
<tr><td>PEAR<br/></td></tr>
<tr><td>1<br/></td></tr>
<tr><td>GRAPE<br/></td></tr>
<tr><td>0<br/></td></tr>
<tr><td>TOMATO<br/></td></tr>
<tr><td>1<br/></td></tr>
DEMO:
Upvotes: -1