in reply to Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
(step by step demo with 'perl -de0')
DB<134> p $in ZBBBCZZ DB<135> x split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'Z' 2 'BBB' 3 'B' 4 'C' 5 'C' 6 'ZZ' 7 'Z'
Alas I couldn't find a way to "forget" the group needed for the backreference \1 °
But this could be turned into something more flexible by applying "pair" functions from List::Util (which were probably not available back then)
DB<137> use List::Util qw/pairkeys pairvalues/ DB<138> x pairkeys split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'BBB' 2 'C' 3 'ZZ' DB<139> x pairvalues split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'B' 2 'C' 3 'Z' DB<140>
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
°) but I didn't search long, what I found is /n modifier, to turn each (...) into (?:...) , but then \1 isn't usable anymore
Tho shorter tye's solution without split fits better here
DB<8> use List::Util qw/pairkeys/ DB<9> $_='ZBBBCZZ' DB<10> x pairkeys /((.)\2*)/g 0 'Z' 1 'BBB' 2 'C' 3 'ZZ' DB<11>
|
---|