Problems? Is your data what you think it is? | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Your solution has a few subtle bugs, try it with:
This happens because $pos is set to -1 after not finding a second 'c', and since you are indexing from $pos + 1, which has become 0, index searches from the start of the string, again. You should also initialize $pos to -1, not zero, for similar reasons (otherwise it ignores the first character in the string). Getting this problem right, without boundary or fencepost erros, employing manual indexing and positioning is remarkably tricky. So much so, that I'm not even sure the following fixed-up code is error-free: I would really like to see if this can be improved upon, assuming the same method is used. In the meantime, here's a slight variant, which I believe to be correct: update: I'm beginning to believe that this problem is the poster-child for unit testing! A more promising possibility... caveat emptor as always: Of course, this one dances around the index problem with a regex. update2: Improving on the original fix...
MeowChow s aamecha.s a..a\u$&owag.print In reply to Re: (bbfu) (another way) Re: Replacing a given character starting with the xth occurence in a string
by MeowChow
|
|