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" }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Split a string based on change of character (also)
by tye (Sage) on Jul 28, 2007 at 07:41 UTC | |
Re: Split a string based on change of character
by moritz (Cardinal) on Jul 28, 2007 at 08:04 UTC | |
by Skeeve (Parson) on Jul 28, 2007 at 13:16 UTC | |
by ikegami (Patriarch) on Jul 28, 2007 at 15:02 UTC | |
by Skeeve (Parson) on Jul 28, 2007 at 15:28 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 29, 2007 at 02:43 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 28, 2007 at 23:47 UTC | |
by Anonymous Monk on Jul 28, 2007 at 08:37 UTC | |
by Anonymous Monk on Jul 28, 2007 at 08:43 UTC |
Back to
Seekers of Perl Wisdom