in reply to Replacing a given character starting with the xth occurence in a string

Another way of doing it:
$n=2; # x-1 $chr="e"; $rep="1"; $a="Terence and Philip are sweet"; while ($a=~s/^((?:.*?$chr){$n})(.*?)$chr/$1$2$rep/) {}

--ZZamboni

Update: At ar0n's suggestion, the last line can also be written as:

1 while ($a=~s/^((?:.*?$chr){$n})(.*?)$chr/$1$2$rep/);
which results in almost exactly what sachmet wrote. I like sachmet's answer better too because it uses only one capturing set of parenthesis.