# Find every location of "th" in string.

Discussion in 'Ruby' started by William James, Dec 12, 2004.

1. ### William JamesGuest

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?

William James, Dec 12, 2004

2. ### Jamis BuckGuest

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.jamisbuck.org/jamis

Jamis Buck, Dec 12, 2004

3. ### David A. BlackGuest

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

David A. Black, Dec 12, 2004
4. ### Jamis BuckGuest

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

http://www.jamisbuck.org/jamis

Jamis Buck, Dec 12, 2004
5. ### Neil StevensGuest

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 -
"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(?)

Neil Stevens, Dec 12, 2004
6. ### William JamesGuest

Thanks. The scan method seems the way to go.

William James, Dec 12, 2004
7. ### Robert KlemmeGuest

"William James" <> schrieb im Newsbeitrag
news:...
> 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

Robert Klemme, Dec 12, 2004
8. ### Glenn ParkerGuest

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/>

Glenn Parker, Dec 12, 2004
9. ### tsGuest

>>>>> "G" == Glenn Parker <> writes:

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

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

Guy Decoux

ts, Dec 12, 2004
10. ### Jamis BuckGuest

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

http://www.jamisbuck.org/jamis

Jamis Buck, Dec 12, 2004
11. ### William JamesGuest

In the Icon example, find() is a generator. You can write your
own generators:

procedure main()
write( fibo() )
write( fibo() )
write( fibo() )

every f := fibo() do
{ writes( f, " " )
writes( "(", fibo(), ") " )
if f > 80 then
break
}
end

procedure fibo()
local old,new,temp
old := 0
new := 1
while 0 do
{ ## Return 'new' and suspend.
suspend new
temp := new
new +:= old
old := temp
}
end

1
1
1
1 (1) 1 (1) 2 (1) 3 (1) 5 (1) 8 (1) 13 (1) 21 (1) 34 (1) 55 (1) 89 (1)
Does Ruby have generators?

William James, Dec 12, 2004
12. ### Michael NeumannGuest

William James wrote:
> In the Icon example, find() is a generator. You can write your
> own generators:
>
> procedure main()
> write( fibo() )
> write( fibo() )
> write( fibo() )
>
> every f := fibo() do
> { writes( f, " " )
> writes( "(", fibo(), ") " )
> if f > 80 then
> break
> }
> end
>
> procedure fibo()
> local old,new,temp
> old := 0
> new := 1
> while 0 do
> { ## Return 'new' and suspend.
> suspend new
> temp := new
> new +:= old
> old := temp
> }
> end
>
> 1
> 1
> 1
> 1 (1) 1 (1) 2 (1) 3 (1) 5 (1) 8 (1) 13 (1) 21 (1) 34 (1) 55 (1) 89 (1)
> Does Ruby have generators?

Not sure what your code above is really doing... in Ruby:

def fib
old, new = 0, 1
loop do
yield new
new, old = new+old, new
end
end

fib do |i|
p i
break if i > 80
end

Regards,

Michael

Michael Neumann, Dec 12, 2004
13. ### Guest

mypatt=/ana/
"banana".scan(/(?=#{mypatt})/) {puts \$`.length}

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.

, Dec 13, 2004
14. ### William JamesGuest

William James, Dec 14, 2004