Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by zeidrik (Scribe)
on May 22, 2001 at 16:58 UTC ( #82212=note: print w/replies, xml ) Need Help??


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

Here is possibly the most compact way (not the fastest yet)
my $s = "Terence and Philip are sweet\n"; my $c = 'e'; my $r = 1; my $n = 3; ($s=~/((.*?)$c){$n}/)&&(($s,$_)=($&,$'))&&(s/$c/$r/g)&&(print $s,$_);
  • Comment on Re: Replacing a given character starting with the xth occurence in a string
  • Download Code

Replies are listed 'Best First'.
(tye)Re: Replacing a given character starting with the xth occurence in a string
by tye (Sage) on May 22, 2001 at 19:53 UTC

    Using $& and $' is a bad idea since it slows down all regexen for the life of that instance of the Perl interpreter (for the life of the script). Perhaps you know that but I don't want casual readers to get the wrong idea about those.

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://82212]
help
Chatterbox?
[ambrus]: (It might be easier to use the callback scalar as a guard, as in $c and &$c(); $c = ();)
[Corion]: ambrus: Hurr - yes, that might happen, as Prima will queue lots of stuff (Well, Windows will)
[Corion]: But I'm really amazed that it just took this little code (plus the idle handler I have in mind and something else dk suggested)
[Corion]: ambrus: I wrote the handler so that it stops the timer if it's supposed to be a one-shot
[Discipulus]: are you using Prima Corion? good morning Corion and ambrus, btw

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2016-12-09 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.