Of course you can use
HTML::Parser and code the table parsing by hand, but I suggest using
HTML::TableContentParser, which is a subclass of
HTML::Parser:
use strict;
use HTML::TableContentParser;
my $html = qq{
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</table>
<table>
<tr><td>11</td><td>12</td><td>13</td></tr>
<tr><td>14</td><td>15</td><td>16</td></tr>
<tr><td>17</td><td>18</td><td>19</td></tr>
</table>
};
my $p = HTML::TableContentParser->new();
my $tables = $p->parse($html);
for my $table (@$tables)
{
print "new table!\n";
for my $row (@{$table->{rows}})
{
print "new row: ";
for my $column (@{$row->{cells}})
{
print "[$column->{data}] ";
}
print "\n";
}
}
That prints:
new table!
new row: [1] [2] [3]
new row: [4] [5] [6]
new row: [7] [8] [9]
new table!
new row: [11] [12] [13]
new row: [14] [15] [16]
new row: [17] [18] [19]
Easy, nice, reliable and clean. Enjoy! ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.