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")