http://www.perlmonks.org?node_id=512842


in reply to Elegant way to split into sequences of identical chars?

Alas! it would be perfect, if no postprocessing was necessary on the match expression result!

Hardly elegant, but along the lines of the many other worthy entries. (Awful hack with $a to avoid declaring it.) Does not have to throw anything away. And probably benchmarks worse given the perl evaluation each time through!

my $line = "xx556xx"; my @list = $line =~ m/((??{ $a = substr( $line, pos($line), 1); $a ? q +uotemeta($a) . '+' : '$' }))/g; print "@list";

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.