Velocity Reviews > Ruby > Find every location of "th" in string.

# Find every location of "th" in string.

William James
Guest
Posts: n/a

 12-12-2004
Find location of every "th" in "the thin man thinks".

In Icon:

every write(find("th", "the thin man thinks"))

In Ruby:

s='the thin man thinks'
t='th'
(0...s.size).each{|x| puts x if s[x,t.size]==t}

Is there a better way?

Jamis Buck
Guest
Posts: n/a

 12-12-2004
On 09:07 Sun 12 Dec , William James wrote:
> Find location of every "th" in "the thin man thinks".
>
> In Icon:
>
> every write(find("th", "the thin man thinks"))
>
> In Ruby:
>
> s='the thin man thinks'
> t='th'
> (0...s.size).each{|x| puts x if s[x,t.size]==t}
>
> Is there a better way?

Perhaps, if I'm understanding what the Icon snippet does:

"the thin man thinks".scan( /th/ ) { p \$& + \$' }

- Jamis

--
Jamis Buck
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.jamisbuck.org/jamis

David A. Black
Guest
Posts: n/a

 12-12-2004
Hi --

On Sun, 12 Dec 2004, Jamis Buck wrote:

> On 09:07 Sun 12 Dec , William James wrote:
>> Find location of every "th" in "the thin man thinks".
>>
>> In Icon:
>>
>> every write(find("th", "the thin man thinks"))
>>
>> In Ruby:
>>
>> s='the thin man thinks'
>> t='th'
>> (0...s.size).each{|x| puts x if s[x,t.size]==t}
>>
>> Is there a better way?

>
> Perhaps, if I'm understanding what the Icon snippet does:
>
> "the thin man thinks".scan( /th/ ) { p \$& + \$' }

I think he wanted the offsets, which could be gotten at like this:

"the thin man thinks".scan(/th/) { p \$~.offset(0)[0] }

David

--
David A. Black
(E-Mail Removed)

Jamis Buck
Guest
Posts: n/a

 12-12-2004
On 09:07 Sun 12 Dec , William James wrote:
> Find location of every "th" in "the thin man thinks".
>
> In Icon:
>
> every write(find("th", "the thin man thinks"))
>
> In Ruby:
>
> s='the thin man thinks'
> t='th'
> (0...s.size).each{|x| puts x if s[x,t.size]==t}
>
> Is there a better way?

Next time I'll read the code sippets better. The following,
I think, will do what you are asking:

"the thin man thinks".scan( /th/ ) { p \$~.begin(0) }

- Jamis

--
Jamis Buck
(E-Mail Removed)
http://www.jamisbuck.org/jamis

Neil Stevens
Guest
Posts: n/a

 12-12-2004
On Sat, 11 Dec 2004 16:05:56 -0800, William James wrote:
> In Ruby:
>
> s='the thin man thinks'
> t='th'
> (0...s.size).each{|x| puts x if s[x,t.size]==t}
>
> Is there a better way?

This is what comes to my mind:

needle = 'th'
x = 0
'the thin man thinks'.split(needle).each do |s|
puts x
x += needle.length + s.length
end

--
Neil Stevens - (E-Mail Removed)
"The world is a dangerous place to live; not because of the people who
are evil, but because of the people who don't do anything about it."
-- Albert Einstein(?)

William James
Guest
Posts: n/a

 12-12-2004
Thanks. The scan method seems the way to go.

Robert Klemme
Guest
Posts: n/a

 12-12-2004

"William James" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ups.com...
> Find location of every "th" in "the thin man thinks".
>
> In Icon:
>
> every write(find("th", "the thin man thinks"))
>
> In Ruby:
>
> s='the thin man thinks'
> t='th'
> (0...s.size).each{|x| puts x if s[x,t.size]==t}
>
> Is there a better way?

>> "the thin man thinks".scan( /th/ ) { puts \$`.length }

0
4
13
=> "the thin man thinks"

Regards

robert

Glenn Parker
Guest
Posts: n/a

 12-12-2004
Robert Klemme wrote:
>>> "the thin man thinks".scan( /th/ ) { puts \$`.length }

> 0
> 4
> 13
> => "the thin man thinks"

scan doesn't really do that well.

"banana".scan(/ana/) { puts \$`.length }
1
=> "banana"

The second overlapping occurence of "ana" is missed.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/>

ts
Guest
Posts: n/a

 12-12-2004
>>>>> "G" == Glenn Parker <(E-Mail Removed)> writes:

G> "banana".scan(/ana/) { puts \$`.length }
G> 1

uln% ruby -e '"banana".scan(/(?=ana)/) { puts \$`.length }'
1
3
uln%

Guy Decoux

Jamis Buck
Guest
Posts: n/a

 12-12-2004
On 00:10 Mon 13 Dec , Glenn Parker wrote:
> Robert Klemme wrote:
> >>>"the thin man thinks".scan( /th/ ) { puts \$`.length }

> >0
> >4
> >13
> >=> "the thin man thinks"

>
> scan doesn't really do that well.
>
> "banana".scan(/ana/) { puts \$`.length }
> 1
> => "banana"
>
> The second overlapping occurence of "ana" is missed.

Well, if you don't mind using a little regexp magic:

>"banana".scan(/a(?=na)/) { puts \$`.length }

1
3
=> "banana"

- Jamis

--
Jamis Buck
(E-Mail Removed)
http://www.jamisbuck.org/jamis