Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating a variable within a s///g

Status
Not open for further replies.

domster

Programmer
Oct 23, 2003
30
0
0
GB
Hi, I'm trying to add an id number to each cell in a bunch of HTML tables, so I'm using the code:

while ($tabdat =~ s/<td /<td id="$idnum" /g) {
$idnum++;
}

This just hangs in a loop, so how do I do this? Apologies if this is really simple, it's Friday afternoon and I've had quite a day. Thanks in advance.
 
In your original HTML file, can I assume that there's no `id' attributes in your <td> tags?
 
It loops because it's repeatedly working on the same "<td".
Why not hit them all at once?
Code:
s/<td /"<td id=\"" . $idnum++ . "\""/ge;
I've not tested that but it seems like a better idea to me.


Trojan.
 
I'm interested in why you want to do this. If you were generating the HTML you could do it more easily then so I'm assuming that this is HTML that you are reading from some website.

There are several techniques for parsing HTML sequentially which would allow you to id the table cells during the parse, rather than before, which may be significantly more efficient.

Would you mind posting some background?

Yours,

fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Can't go into too much detail, but they're HTML tables that have been created manually for an application that runs under Mozilla. I didn't spec for id's when they were created, but now we need the id's in there. I just need a quick & dirty way of doing it, I don't really have the time to get into CPAN modules or anything like that.

I'll check out Trojan's method above and let you know.
 
Thanks for the info.

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top