in reply to Continuing after replacing nth occurrence
First off the obligatory advice to those new to Perl: always use strictures (use strict; use warnings;)!
Secondly, the obligatory advice to those who haven't yet learned the folly of rolling their own HTML parsing code - Don't. Use CPAN HTML modules instead. In this case you could try HTML::TableExtract and HTML::Table:
use strict; use warnings; use HTML::Table; use HTML::TableExtract; my $tbltext = <<'END_TBL'; <table> <tr><td>1</td><!-- deleted! <td>2</td>!--><td>3</td><td>4</td></tr +> <tr><td>one</td><td>two</td><td>three</td></tr> </table> END_TBL my $tableEx = HTML::TableExtract->new (); $tableEx->parse ($tbltext); my @rows = $tableEx-> rows (); for my $row (@rows) { next if @$row < 3; # Not interested in short rows $row->[2] = 'newText'; } my $tableOut = HTML::Table->new (); $tableOut->addRow (@$_) for @rows; $tableOut->print ();
Prints:
<table> <tr><td>1</td><td>3</td><td>newText</td></tr> <tr><td>one</td><td>two</td><td>newText</td></tr> </table>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Continuing after replacing nth occurrence
by knlst8 (Initiate) on Mar 10, 2009 at 11:13 UTC |