Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Replacing a given character starting with the xth occurence in a string

by ZZamboni (Curate)
on May 21, 2001 at 04:16 UTC ( [id://81887]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://81887]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-19 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found