Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Split a string based on change of character

by moritz (Cardinal)
on Jul 28, 2007 at 08:04 UTC ( #629265=note: print w/ replies, xml ) Need Help??


in reply to Split a string based on change of character

I don't know if you can use a split here, because your pattern may not consume characters to achieve what you want.

I've tried this one: m/((?<=.))(?!\1)/ which should be "a position before which there is a character, and after that a different character", but it doesn't work.

Can anybody tell me why this doesn't match the string 'aaabbbc'?

Update: Zoffix told me on IRC that the right thing would be m/(?<=(.))(?!\1)/ (because the assertion is zero-width), however that doesn't work in split as well, because it returns the captured character:

$ perl -MData::Dumper -wle '$Data::Dumper::Indent=0; $_="aaabbc"; prin +t Dumper([split m/(?<=(.))(?!\1)/]);' $VAR1 = ['aaa','a','bb','b','c','c'];

This way you had to discard every second item of the returned list - not pretty either ;-)


Comment on Re: Split a string based on change of character
Select or Download Code
Re^2: Split a string based on change of character
by Anonymous Monk on Jul 28, 2007 at 08:37 UTC
    <Zoffix> eval: $_='zyxxaabbbcccccc'; push @a, $1 while s/((.)\2*)//;[@ +a] <_ZofBot> Zoffix: ['z','y','xx','aa','bbb','cccccc']

    20070730 Janitored by Corion: Added code tags, as per Writeup Formatting Tips

      Or even: $_='zyxxaabbbcccccc'; push @a, $1 while /((.)\2*)/g; Which doesn't destroy original data.
Re^2: Split a string based on change of character
by Skeeve (Vicar) on Jul 28, 2007 at 13:16 UTC
    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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://629265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-09-20 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls