eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:

For a string 'ABBBCC', I want to produce a list ('A', 'BBB', 'CC'). That is, break the string into pieces based on change of character.

My Perl is getting a bit rusty and I found myself struggling today with this simple problem. Though I've found a solution, shown below, there is probably a better way to do it, hence my question. Apologies if this is a FAQ.

use strict; use warnings; my $str = 'AAABBCCCC'; # For str 'AAABBCCCC', I want to produce a list ('AAA', 'BB', 'CCCC'). # This works ... but is there a better way to do it? my $i = 0; # $i is used to filter out the captured $1 fields my @x = grep { ++$i % 2 } split(/(?<=(.))(?!\1)/, $str); for my $e (@x) { print "e='$e'\n" }