### Re^2: Split a string based on change of character

by Skeeve (Vicar)
 on Jul 28, 2007 at 13:16 UTC ( #629290=note: print w/replies, xml ) Need Help??

You can use split:
```my \$string= 'AAABBBCCCDD';
my \$i=0;
my @words= grep \$i=!\$i,split /(?<=(.))(?!\1)/,\$string;
print join "\n",@words,'';

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e

Replies are listed 'Best First'.
Re^3: Split a string based on change of character
by ikegami (Pope) on Jul 28, 2007 at 15:02 UTC
I never thought of
```grep \$i=!\$i,
before. I usually use
```grep \$i^=\$i,

Yours is easier to understand, though.

Update: Yeah, I mean grep \$i^=1,.

But \$i^=\$i makes \$i 0 at once. So it's different to \$i=!\$i

Back in the ancient times of C64 Basic I used j=1-j

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e

I think ikegami meant \$i^=1, i.e.:

```my @words = grep \$i^=1, split /(?<=(.))(?!\1)/, \$string;
which does work.

Re^3: Split a string based on change of character
by eyepopslikeamosquito (Chancellor) on Jul 28, 2007 at 23:47 UTC

As you might expect, I was tempted to (ab)use the magical \$| flip-flop variable:

```my @words = grep --\$|, split /(?<=(.))(?!\1)/, \$string;
but was able to restrain myself. :-)

Create A New User
Node Status?
node history
Node Type: note [id://629290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-08-16 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (167 votes). Check out past polls.

Notices?