Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Replace the nth occurence

by rcrews (Novice)
on Nov 21, 2012 at 05:37 UTC ( #1004840=note: print w/ replies, xml ) Need Help??


in reply to Replace the nth occurence

The built-in substr function does it:

my $count = 3; my $str = "a,b,c,d"; substr $str, $count, 1, '|'; print $str;


Comment on Re: Replace the nth occurence
Download Code
Re^2: Replace the nth occurence
by Ratazong (Prior) on Nov 21, 2012 at 05:54 UTC
    Have you tried to replace the 2nd comma in the following string using your code? Seems there is room for improvement...
    my $str = "aa,bb,cc,dd";
      It doesn't even work on the example string for any count other than three--it was just a coincidence that it happened to be in the right position in that case.


      When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-09-21 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls