Forum: Ruby Array.find with index

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rob R. (Guest)
on 2009-04-24 08:08
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
Loga G. (Guest)
on 2009-04-24 08:57
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]
lasitha (Guest)
on 2009-04-24 09:40
(Received via mailing list)
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
Loga G. (Guest)
on 2009-04-24 09:52
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
Heesob P. (Guest)
on 2009-04-24 09:55
(Received via mailing list)
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.
lasitha (Guest)
on 2009-04-24 10:22
(Received via mailing list)
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.
This topic is locked and can not be replied to.