Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Split a string based on change of character

by moritz (Cardinal)
on Jul 28, 2007 at 08:04 UTC ( [id://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 ;-)

Replies are listed 'Best First'.
Re^2: Split a string based on change of character
by Skeeve (Parson) 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
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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found