Splitting on a change, Challenge 20 Task 1

I like reading the Perl Weekly Challenge even if I rarely participate.

It struck me that task 1 of Week 20 asked "to accept a string from command line and split it on change of character" - but every solution that I read in the recap looked for runs of the same character instead of the literal interpretation of the challenge.

Then I found, it was a dead end for me...

$ perl -E 'say join " ",split /(.)(?!\1)/,scalar '
ABBCDEEF
 A B B  C  D E E  F

Ah, I want zero-width on each side, but I want to both use capture for \1 but NOT also have "split" keep the capture, can't have it both ways...

$ perl -E 'say join " ",split /(?<=(.))(?!\1)/,scalar '
ABBCDEEF
A A BB B C C D D EE E F F

Perl6's expressiveness won for me, but I think this could be cleaner, and it has a spurious empty string at the beginning. Ideas for improvement, dear reader?

say "ABBCDEEF".split(/<?before (.) {} :my $c=$0;><!after $c> /).perl
("", "A", "BB", "C", "D", "EE", "F").Seq

1 Comment

Congratulation on your first blog about your solution to Perl Weekly Challenge. I hope to see more.

Leave a comment

About Yary

user-pic Programming for decades, and learning something new every day.