As for my solution, I was focused on the sample input given, I didn't think of the more general problem. But you're right about this.
As for your script, it works with a small alteration:
If you increment $i in a seperate statement, the whole block evaluates to the value of $i und you get$string =~ s/($pattern)/$i++ == $n ? $better : $1/ge;
Ok, now we're having fun :-)...abacab 0b1c2b
Andreas (waiting for the chinese food delivery service ...)