Array.find with index


#1

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


#2

Rob R. wrote:

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[i]>20?c<<i:nil }
=> [10, 20, 30, 40, 50, 60]


#3

On Fri, Apr 24, 2009 at 9:38 AM, Rob R. removed_email_address@domain.invalid
wrote:

How do I get the matched indices? That 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


#4

lasitha wrote:

On Fri, Apr 24, 2009 at 9:38 AM, Rob R. removed_email_address@domain.invalid
wrote:

How do I get the matched indices? �That 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

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


#5

Hi,

2009/4/24 Rob R. removed_email_address@domain.invalid:

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.

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

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

Regards,
Park H.


#6

On Fri, Apr 24, 2009 at 11:22 AM, Loga G.
removed_email_address@domain.invalid wrote:

In the URL, they specified the following:
    from (irb):13:in `rindex’
    from (irb):13
    from (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 == 2 }’
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8]
1

$: ruby -ve ‘puts [1, 2, 3].rindex {|e| e == 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.