[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Index of the n-th occurrence

Marco

12/26/2007 7:53:00 AM

Hi, some newbie questions...

1) How can I obtain all the indices of the occurrences of a pattern in a
string (and not only the index of the first one using "index")? And the
index of the n-th occurrence?

2) What's the simplest way to select elements of a vector,on the basis of a
condition on a second vector of the same length?

Thanks,

Marco


4 Answers

Stefano Crocco

12/26/2007 9:28:00 AM

0

Alle mercoled=EC 26 dicembre 2007, Marco ha scritto:
> Hi, some newbie questions...
>
> 1) How can I obtain all the indices of the occurrences of a pattern in a
> string (and not only the index of the first one using "index")? And the
> index of the n-th occurrence?

I'm sure there's a better solution, but I can't think of it right now. For =
the=20
first part of your question, notice that String#index (unlike Array#index)=
=20
accept an optional offset argument. You can then do something like:

str =3D"a test string"

positions =3D []

pos =3D -1

while (pos =3D s.index(your_pattern, pos + 1))
positions << pos
end

After running this code, positions will contain the indices of all the=20
occurrences of your_pattern (which can be any argument String#index accepts=
)=20
inside str.

To get the index of the n-th occurrence, you can extract the n-th element f=
rom=20
such an array, or add a counter to the while cycle.


> 2) What's the simplest way to select elements of a vector,on the basis of=
a
> condition on a second vector of the same length?

It depends on what your needs exactly are. Suppose you have the two arrays=
=20

a1=3D%w[a b c d]
a2=3D[2, 3, 5, 6]

and want to get the letters in a1 in the same places of even numbers in a2=
=20
(i.e, ["a", "d"]).

These are some approaches (you need to do require 'enumerator' before all o=
f=20
them):

a1.enum_for(:each_with_index).select{|e, idx| a2[idx]%2=3D=3D0}
=3D> [["a", 0], ["d", 3]]

This returns an array of the pairs of the letter and its index. You can the=
n=20
extract the letters with map.



a1.enum_for(:each_with_index).inject([]){|res, i|=20
a2[i[1]]%2 =3D=3D 0 ? res << i[0] : res }
=3D> ["a", "d"]



a1.enum_for(:each_with_index).map{|e, idx| a2[idx]%2=3D=3D0 ? e : nil}.comp=
act

This will not work if a1 can contain nil elements (because they also would =
be=20
removed by nil).

I hope this helps

Stefano


Sebastian Hungerecker

12/26/2007 9:41:00 AM

0

Marco wrote:
> 2) What's the simplest way to select elements of a vector,on the basis of a
> condition on a second vector of the same length?

res=[]
[1,2,3,4].zip([2,4,9,16]) {|x,y| res << x if y==x*2}
res #=> [1,2]


HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

William James

12/26/2007 11:06:00 AM

0

On Dec 26, 1:53 am, "Marco" <marv...@tin.it> wrote:
> Hi, some newbie questions...
>
> 1) How can I obtain all the indices of the occurrences of a pattern in a
> string (and not only the index of the first one using "index")? And the
> index of the n-th occurrence?

Why do you want these indices? There may be an easier
way to accomplish your goal. Ruby isn't APL.

a = []
==>[]
"the thug's thews".scan(/.*?(th)/){ a << $~.begin(1) }
==>"the thug's thews"
a
==>[0, 4, 11]

Tomaso

12/27/2007 7:13:00 PM

0

On Dec 26, 8:53 am, "Marco" <marv...@tin.it> wrote:
> Hi, some newbie questions...
>
> 1) How can I obtain all the indices of the occurrences of a pattern in a
> string (and not only the index of the first one using "index")? And the
> index of the n-th occurrence?

Hi
perhaps you can find something useful in ruby core doc RegExp/
MatchData
it seems that the method offset is good for you

mtch.offset(n) => array

Returns a two-element array containing the beginning and ending
offsets of the nth match.

Tom

>
> 2) What's the simplest way to select elements of a vector,on the basis of a
> condition on a second vector of the same length?
>
> Thanks,
>
> Marco