[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Split a string based on change of character

Andrew Savige

8/11/2007 6:06:00 AM

--- Peña, Botp <botp@delmonte-phil.com> wrote:
> 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)

Actually, I'm not super happy with either solution. :-)

The annoyance with the regex solution:

s = "ZBBBCZZ"
x = s.scan(/((.)\2*)/).map {|i| i[0]}

is that you capture the backref (when you don't really want to), only
to discard it in the map, which seems a bit awkard and inefficient.
However, I can't see any way around this owing to the regex semantics
of returning all fields in parens (one has the same problem in Perl
and Python, BTW). [If there were a way to specify a non-capturing
back-ref, that would do the trick.]

> in 1.9 or the upcoming ruby, it keeps getting better and better and may look
> like this,
>
> s = "ZBBBCZZ"
> x = s.split('').group_by{|x| x}.entries

My reading of:

http://eigenclass.org/hiki.rb?Changes+i...

indicates that Enumerable#group_by can't work because it would seem to lose
the ordering and, grouping by key, will have only one group for 'Z' above,
when I want two distinct groups. (I would be delighted to be proved wrong,
however).

I also scanned the Facets library but didn't find anything obvious.

Cheers,
/-



____________________________________________________________________________________
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedst...