I'm relatively new to perl, and could use some help solving a substitution problem.
So I'm trying to make a change to every cell in the nth column of an HTML table. Those of you familiar with HTML will know that HTML doesn't "think" in columns, so I have to make a change to the nth cell in each row. Try as I might, I can only manage to change the first row.
I'm thinking something like this:
#this while loop isn't right, but I don't know what is...;
while ($tbltext =~ /<tr.*?>/cg) {
$counter = 1; #reset the counter;
$tbltext =~ s/(<td.*?\/td>)/
++$counter == $nth #is this the nth cell?
? "${1}newtext" #if yes, add "newtext"
: $1 #otherwise, leave it
/ge;
$tbltext =~ /<\/tr>/cg; #find the end of the row
} #exit the loop
I can't get a 'while' loop to work. In addition, I've tried 'for' and 'foreach' loops, and every combination of \g \G and \cg that I can think of. It always finds only the first row tag, and then counts the cells and substitutes for the nth cell in the first row. Any sort of loop that I put around it only makes the substitution in the first row in every pass through the loop.
I'm betting there's a really simple answer that I just haven't stumbled on yet. Can anyone here help?
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.