Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Replace the nth occurence

by Kenosis (Priest)
on Nov 21, 2012 at 07:22 UTC ( #1004858=note: print w/replies, xml ) Need Help??


in reply to Replace the nth occurence

I like your solution for replacing the nth occurrence of a character within a string. I don't know whether the following is a 'better suggestion,' but what about initializing $n (for the nth) to the desired occurrence:

use strict; use warnings; # Replace second comma my $n = 2; my $str = 'a,b,c,d'; $str =~ s/(,)/!--$n ? '|' : $1/ge; print $str;

Output:

a,b|c,d

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004858]
help
Chatterbox?
[Discipulus]: practical use for?
[marto]: finding bugs, either in the CPU layer or hypervisor, finding undocumented instructions
[zentara]: are you talikng about perf?
[Discipulus]: i miss what an undocumented instructions can be, and how can be used
[zentara]: perf

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-07-28 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (428 votes). Check out past polls.