lazo lazo - 1 month ago 7
Perl Question

Remove entire row from table with HTML::TableExtract

Is there a way to remove an entire row (html tags 'n all) from an HTML Table with

HTML::TableExtract
?

Mucking around with the sample code from CPAN, this is what I've tried so far:


use HTML::TableExtract qw(tree);

my $te = HTML::TableExtract->new( headers => [qw(name type members)] );

# get $html_string out of a file...

$te->parse($html_string);

my $table = $te->first_table_found();
my $table_tree = $table->tree;
$table_tree->row(4)->replace_content('');
my $document_tree = $te->tree;
my $document_html = $document_tree->as_HTML;

# write $document_html to a file ...


Now, as the name suggests, 'replace_content()' in the line
$table_tree->row(4)->replace_content('');
removes the content of row 4, but the row itself remains in markup. I need to get the tags and everything in-between removed as well.

Any ideas?

mrk mrk
Answer

What you want is the parent and delete methods

See the docs for HTML::Element and for HTML::Element::delete

UPDATE

Ok, click that checkmark and mark this one as answered....Here it is:

my($p) = $table_tree->row(4)->parent();
$p->delete;

Also, NOTE, you need the () parens around $p! If you don't have parens don't get back a reference.

For me, with the above Perl code working on this HTML,

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td> <td>row1</td></tr>
   <tr><td>row2</td><td>row2</td> <td>row2</td></tr>
   <tr><td>row3</td><td>row3</td> <td>row3</td></tr>
   <tr><td>row4</td><td>row4</td> <td>row4</td></tr>
</table>

I get this as a result of printing $document_html

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td><td>row1</td></tr>
   <tr><td>row2</td><td>row2</td><td>row2</td></tr>
   <tr><td>row3</td><td>row3</td><td>row3</td></tr>
</table>

Notice that there is no empty <tr></tr>