From: Caleb Clausen on
On 4/13/10, Alex DeCaria <alex.decaria(a)millersville.edu> wrote:
> Andrea Dallera wrote:
>> Hei,
>>
>> s = 'abcde_abcde_abcde'
>> m = s.scan(/b/)
>> p m # ['b','b','b']
>>
>> HTH
>
> Thanks Andrea. But is there a way to also find the locations (indexes)
> of the 'b' characters?

I think this works:

s = 'abcde_abcde_abcde'
i=0
while i=s.index('b',i)
do_something_with i
end

From: Andrea Dallera on
Nope, tried it.

--
Andrea Dallera
http://github.com/bolthar/freightrain
http://usingimho.wordpress.com


On Wed, 2010-04-14 at 02:27 +0900, Caleb Clausen wrote:
> s = 'abcde_abcde_abcde'
> i=0
> while i=s.index('b',i)
> do_something_with i
> end


From: botp on
On Wed, Apr 14, 2010 at 1:32 AM, Andrea Dallera
<andrea(a)andreadallera.com> wrote:
> Nope, tried it.
>
> --
> Andrea Dallera
> http://github.com/bolthar/freightrain
> http://usingimho.wordpress.com
>
>
> On Wed, 2010-04-14 at 02:27 +0900, Caleb Clausen wrote:
>> s = 'abcde_abcde_abcde'
>>   i=0
>>   while i=s.index('b',i)
>>      do_something_with i
>>   end
>

what do you mean it does not work?

btw, you can also use stringscan. it's fast.

> s=StringScanner.new "this is a test string"
=> #<StringScanner 0/21 @ "this ...">

> p s.pos while s.scan_until /s/
4
7
13
16
=> nil


pls do not top post.

kind regards -botp

From: Robert Klemme on
On 13.04.2010 19:01, Alex DeCaria wrote:
> Andrea Dallera wrote:
>> Hei,
>>
>> s = 'abcde_abcde_abcde'
>> m = s.scan(/b/)
>> p m # ['b','b','b']

> Thanks Andrea. But is there a way to also find the locations (indexes)
> of the 'b' characters?

irb(main):012:0> s="a "*5
=> "a a a a a "
irb(main):013:0> s.scan(/a+/) { p $~, $~.offset(0) }
#<MatchData "a">
[0, 1]
#<MatchData "a">
[2, 3]
#<MatchData "a">
[4, 5]
#<MatchData "a">
[6, 7]
#<MatchData "a">
[8, 9]
=> "a a a a a "
irb(main):014:0>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/