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>

True laziness is hard work

In reply to Re: Continuing after replacing nth occurrence by GrandFather
in thread Continuing after replacing nth occurrence by knlst8

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.