Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Replace the nth occurence

by LanX (Canon)
on Nov 21, 2012 at 23:16 UTC ( #1005026=note: print w/ replies, xml ) Need Help??


in reply to Replace the nth occurence

++AnomalousMonk for demonstrating what \K is good for! =)

Maybe I'm boring ... but I prefer more explicit code with split-and-join over regex-acrobatics:

my $nth = 4; my $str = 'a,bb,ccc,dddd,eeeee,ffffff'; my @str= split /,/,$str; $str = join ( "," , @str[0..$nth-1] ) . "|" . join ( "," , @str[$nth.. +$#str] ); print qq{'$str'};

Cheers Rolf


Comment on Re: Replace the nth occurence
Download Code
Replies are listed 'Best First'.
Re^2: Replace the nth occurence
by AnomalousMonk (Abbot) on Nov 23, 2012 at 06:22 UTC
    ... I prefer more explicit code ... over regex-acrobatics ...

    And, in general, so do I. However, I spent so much effort figuring out regexes and they offer so many bright doodads and shiny gewgaws that my first reaction to a question like the OP is "Hey, I should be able to do this (or do it better) with a regex like..." So I put together a regex and it works – sort of; and I see how I can fix it by putting in a positive look-ahead here; and that makes it better, but still not quite, but if I put in an alternation there...; oops, now it doesn't work at all; oh, I see, that should have been...; that's a lot better, but there's still this corner case... And so it goes. But in the end I learn a little more about regexes, which makes me a little more likely to turn to them in the future... This stuff isn't syntactic sugar, it's syntactic heroin!

      > This stuff isn't syntactic sugar, it's syntactic heroin!

      LOL ... true so true.

      I really miss the time when I needed to optimize stuff in 68000-assembler.

      So for me hacking regexes is a methadone program! ;-)

      Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2015-07-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls