[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

What's the best way to split this kind of string?

Sam Kong

3/13/2006 7:58:00 AM

Hi!

I'm trying to make a routine to make a Morris Number Sequence.
(http://www.ocf.berkeley.edu/~stoll/number_seq...)
I need to split a string in the following way, for example.

"111223133" => ["111", "22", "3", "1", "33"]

I can loop thru the string by each character, comparing with the
previous character.
But is there a quick and easy way?
Probably a regular expression?

Thanks.

Sam

5 Answers

Jim Weirich

3/13/2006 8:18:00 AM

0

Sam Kong wrote:
> Hi!
>
> I'm trying to make a routine to make a Morris Number Sequence.
> (http://www.ocf.berkeley.edu/~stoll/number_seq...)
> I need to split a string in the following way, for example.
>
> "111223133" => ["111", "22", "3", "1", "33"]
>
> I can loop thru the string by each character, comparing with the
> previous character.
> But is there a quick and easy way?
> Probably a regular expression?

This seems to work:

"111223133".scan(/1+|2+|3+|4+|5+|6+|7+|8+|9+|0+/)
=> ["111", "22", "3", "1", "33"]

--
-- Jim Weirich



--
Posted via http://www.ruby-....


Ross Bamford

3/13/2006 8:24:00 AM

0

On Mon, 2006-03-13 at 16:58 +0900, Sam Kong wrote:
> Hi!
>
> I'm trying to make a routine to make a Morris Number Sequence.
> (http://www.ocf.berkeley.edu/~stoll/number_seq...)
> I need to split a string in the following way, for example.
>
> "111223133" => ["111", "22", "3", "1", "33"]
>
> I can loop thru the string by each character, comparing with the
> previous character.
> But is there a quick and easy way?
> Probably a regular expression?

Maybe something like:

s.scan(/(\d)(\1*)/).map! { |e| e.join }
# => ["111", "22", "3", "1", "33"]

or:

s.scan(/0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/)
# => ["111", "22", "3", "1", "33"]

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



William James

3/13/2006 9:54:00 AM

0


Sam Kong wrote:
> Hi!
>
> I'm trying to make a routine to make a Morris Number Sequence.
> (http://www.ocf.berkeley.edu/~stoll/number_seq...)
> I need to split a string in the following way, for example.
>
> "111223133" => ["111", "22", "3", "1", "33"]
>
> I can loop thru the string by each character, comparing with the
> previous character.
> But is there a quick and easy way?
> Probably a regular expression?
>
> Thanks.
>
> Sam

"111223133".scan(/(.)(\1*)/).map{|x|x.join}

Thiago Arrais

3/13/2006 11:20:00 AM

0

On 3/13/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> Maybe something like:
>
> s.scan(/(\d)(\1*)/).map! { |e| e.join }
> # => ["111", "22", "3", "1", "33"]

Hmm.... Clever

--
Thiago Arrais


Andrew Johnson

3/13/2006 5:44:00 PM

0

On Mon, 13 Mar 2006 17:23:56 +0900, Ross Bamford <rossrt@roscopeco.co.uk>
wrote:
[snip]
> Maybe something like:
>
> s.scan(/(\d)(\1*)/).map! { |e| e.join }
> # => ["111", "22", "3", "1", "33"]
>
> or:
>
> s.scan(/0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/)
> # => ["111", "22", "3", "1", "33"]

Or:

s.scan(/((.)\2*)/).transpose[0]

Boy, are my fingers glad to save those keystrokes :-)

andrew

--
Andrew L. Johnson http://www.s...
They're not soaking, they're rusting!
-- my wife (on my dishwashing habits)