From: dblack@... Date: 2007-08-11T21:44:29+09:00 Subject: Re: Split a string based on change of character --1926193751-250232837-1186836270=:3923 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-250232837-1186836270=:3923" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-250232837-1186836270=:3923 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sat, 11 Aug 2007, Pe=F1a, Botp wrote: > From: Andrew Savige [mailto:ajsavige@yahoo.com.au] > # s =3D "ZBBBCZZ" > # x =3D s.scan(/((.)\2*)/).map {|i| i[0]} > > when it comes to string patterns like this, nothing beats regex > > # import itertools > # s =3D "ZBBBCCZZ" > # x =3D [''.join(g) for k, g in itertools.groupby(s)] > # Does anyone know if Ruby has a similar library to Python's itertools? > > hmm, you seem to like this than your previous regex+map solution, why? (i= ask because i prefer your first solution --not that it's ruby) > > in 1.9 or the upcoming ruby, it keeps getting better and better and may l= ook like this, > > s =3D "ZBBBCZZ" > x =3D s.split('').group_by{|x| x}.entries > > or possibly to > > x =3D s.split('').group_by.entries I'm going to have to get special glasses that can read invisible ink.... :-) David --=20 * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --1926193751-250232837-1186836270=:3923-- --1926193751-250232837-1186836270=:3923--