Find every location of "th" in string.

W

William James

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?
 
J

Jamis Buck

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
 
D

David A. Black

Hi --

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
 
J

Jamis Buck

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
 
N

Neil Stevens

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
 
R

Robert Klemme

William James said:
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
 
G

Glenn Parker

Robert said:
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.
 
T

ts

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

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



Guy Decoux
 
J

Jamis Buck

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
 
W

William James

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?
 
M

Michael Neumann

William said:
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
 
L

lostboard2001

oh, right, lookaheads:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top