[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Find every location of "th" in string.

William James

12/12/2004 12:06:00 AM

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?

13 Answers

Jamis Buck

12/12/2004 12:16:00 AM

0

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
jgb3@email.byu.edu
http://www.jamisbuck...



dblack

12/12/2004 12:23:00 AM

0

Jamis Buck

12/12/2004 12:23:00 AM

0

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
jgb3@email.byu.edu
http://www.jamisbuck...



Neil Stevens

12/12/2004 12:31:00 AM

0

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 - neil@hakubi.us
"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

12/12/2004 1:34:00 AM

0

Thanks. The scan method seems the way to go.

Robert Klemme

12/12/2004 11:21:00 AM

0


"William James" <w_a_x_man@yahoo.com> schrieb im Newsbeitrag
news:1102809956.300284.26710@f14g2000cwb.googlegroups.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

12/12/2004 3:11:00 PM

0

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.tetrafoi...


ts

12/12/2004 3:20:00 PM

0

>>>>> "G" == Glenn Parker <glenn.parker@comcast.net> writes:

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

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



Guy Decoux


Jamis Buck

12/12/2004 3:20:00 PM

0

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
jgb3@email.byu.edu
http://www.jamisbuck...



William James

12/12/2004 9:42:00 PM

0

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?