Array.find with index

R

Rob Redmon

How do I find matching expressions and return the match and the index of
the match? Actually, I'd be happy with just the index.

Basically, I want the Ruby way to:
1) Given scalar s
2) Given array a1 = [] which monotonically increases (e.g.
[10,11,12,15,16,..])
3) Find the two elements in a1 who are nearest neighbors to the scalar
s. I need the indices more than the values.

Clearly, I can find the nearest values with this:
[10,20,30,40,50].find_all{|item| item >= 25 }.first => 30
[10,20,30,40,50].find_all{|item| item <= 25 }.last
=> 20

How do I get the matched indices? That is, without ugly loops.

R
 
L

Loga Ganesan

Rob said:
How do I find matching expressions and return the match and the index of
the match? Actually, I'd be happy with just the index.

Basically, I want the Ruby way to:
1) Given scalar s
2) Given array a1 = [] which monotonically increases (e.g.
[10,11,12,15,16,..])
3) Find the two elements in a1 who are nearest neighbors to the scalar
s. I need the indices more than the values.

Clearly, I can find the nearest values with this:
[10,20,30,40,50].find_all{|item| item >= 25 }.first => 30
[10,20,30,40,50].find_all{|item| item <= 25 }.last
=> 20

How do I get the matched indices? That is, without ugly loops.

R


Hi friend,
I think that in find_all method, we don't have any provision
to
extract indices alone. But there could be enormous way to achieve your
target.
I suggest you the below code:

irb(main):064:0> a=[10, 20, 30, 40, 50, 60]
=> [10, 20, 30, 40, 50, 60]
irb(main):065:0> c=[];a.each_index{|i| a>20?c<<i:nil }
=> [10, 20, 30, 40, 50, 60]
 
L

lasitha

[...]
Find the two elements in a1 who are nearest neighbors to the scalar
s. =A0I need the indices more than the values.

Clearly, I can find the nearest values with this:
[10,20,30,40,50].find_all{|item| item >=3D 25 }.first =3D> 30
[10,20,30,40,50].find_all{|item| item <=3D 25 }.last
=3D> 20

How do I get the matched indices? =A0That is, without ugly loops.

#index and #rindex in their block forms come to mind:
http://ruby-doc.org/core-1.9/classes/Array.html#M001974

cheers,
lasitha
 
L

Loga Ganesan

lasitha said:
#index and #rindex in their block forms come to mind:
http://ruby-doc.org/core-1.9/classes/Array.html#M001974

cheers,
lasitha

In the URL, they specified the following:
a.rindex{|x|x=="b"}

But the rindex method doesn't provide the syntax of passing the blocks.
I had
tried the following and it thrown the error.

irb(main):012:0> a=[10,20,30,40,50]
=> [10, 20, 30, 40, 50]
irb(main):013:0> a.rindex{|x| x>20}
ArgumentError: wrong number of arguments (0 for 1)
from (irb):13:in `rindex'
from (irb):13
from (null):0
 
H

Heesob Park

Hi,

2009/4/24 Rob Redmon said:
How do I find matching expressions and return the match and the index of
the match? =C2=A0Actually, I'd be happy with just the index.

Basically, I want the Ruby way to:
1) Given scalar s
2) Given array a1 =3D [] which monotonically increases (e.g.
[10,11,12,15,16,..])
3) Find the two elements in a1 who are nearest neighbors to the scalar
s. =C2=A0I need the indices more than the values.

Clearly, I can find the nearest values with this:
[10,20,30,40,50].find_all{|item| item >=3D 25 }.first =3D> 30
[10,20,30,40,50].find_all{|item| item <=3D 25 }.last
=3D> 20

How do I get the matched indices? =C2=A0That is, without ugly loops.

irb(main):001:0> a =3D [10, 20, 30, 40, 50, 60]
=3D> [10, 20, 30, 40, 50, 60]
irb(main):004:0> (0...a.length).select{|x| a[x]>=3D25}.first
=3D> 2
irb(main):005:0> (0...a.length).select{|x| a[x]<=3D25}.last
=3D> 1

In ruby 1.9.x
irb(main):002:0> a.index{|x| x<=3D25}
=3D> 2
irb(main):003:0> a.rindex{|x| x>=3D25}
=3D> 1

Regards,
Park Heesob
 
L

lasitha

lasitha said:
#index and #rindex in their block forms come to mind:
http://ruby-doc.org/core-1.9/classes/Array.html#M001974

cheers,
lasitha

In the URL, they specified the following:
=C2=A0 =C2=A0 a.rindex{|x|x=3D=3D"b"}

But the rindex method doesn't provide the syntax of passing the blocks.
I had
tried the following and it thrown the error.

irb(main):012:0> a=3D[10,20,30,40,50]
=3D> [10, 20, 30, 40, 50]
irb(main):013:0> a.rindex{|x| x>20}
ArgumentError: wrong number of arguments (0 for 1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):13:in `rindex'
=C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):13
=C2=A0 =C2=A0 =C2=A0 =C2=A0from (null):0

Hmm, the doco was for 1.9, but it works for 1.8.7 too:

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e =3D=3D 2 }'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8]
1

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e =3D=3D 2 }'
ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
1

I don't have 1.8.6 to test on this box, i'm afraid.

solidarity,
lasitha.
 

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

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top