Find every location of "th" in string.

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

  1. 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
    #1
    1. Advertising

  2. William James

    Jamis Buck Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. William James

    Jamis Buck Guest

    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
    #4
  5. William James

    Neil Stevens Guest

    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
    #5
  6. Thanks. The scan method seems the way to go.
    William James, Dec 12, 2004
    #6
  7. "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
    #7
  8. William James

    Glenn Parker Guest

    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
    #8
  9. William James

    ts Guest

    >>>>> "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
    #9
  10. William James

    Jamis Buck Guest

    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
    #10
  11. 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
    #11
  12. 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
    #12
  13. William James

    Guest

    oh, right, lookaheads:

    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
    #13
  14. William James, Dec 14, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Dalessandro
    Replies:
    0
    Views:
    591
    Luke Dalessandro
    Jan 15, 2006
  2. Gary Wessle

    string.find first before location

    Gary Wessle, May 2, 2006, in forum: Python
    Replies:
    8
    Views:
    356
    Kent Johnson
    May 3, 2006
  3. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,434
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  4. fishfry

    Find location of substring in string?

    fishfry, Sep 24, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    104
    Bart Lateur
    Sep 24, 2004
  5. Sunshine
    Replies:
    0
    Views:
    500
    Sunshine
    Sep 11, 2007
Loading...

Share This Page