# Array index of first bigger number

**URL:** <https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765>\
**Category:** Ruby\
**Created:** [June 2, 2007, 9:36am UTC](https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765 "2007-06-02T09:36:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 2, 2007, 9:36am UTC](https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765/1 "2007-06-02T09:36:04Z")

</div>

Is there a simple function (I solve it w a loop… C-minded) to find the  
index of the first bigger element in an array

limit mini is first\_element , if item \< last\_element  
item = -2.23  
anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first\_bigger\_element =  
0.0 index 0

item = 2.85  
anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first\_bigger\_element =  
5.0 index 2

item = 12.55  
anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first\_bigger\_element =  
15.0 index 4

limit maxi is last\_element , if item \> last\_element  
item = 36.59  
anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first\_bigger\_element =  
25.0 index 5

can it be writtent in just one line or a loop is unavoidable ?

thanks

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 2, 2007, 9:55am UTC](https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765/2 "2007-06-02T09:55:05Z")

</div>

Alle sabato 2 giugno 2007, Josselin ha scritto:

> 5.0 index 2  
> can it be writtent in just one line or a loop is unavoidable ?
> 
> thanks
> 
> joss

You can do:

anArray.index(anArray.find{|i| i \> item})

anArray.find will return the first element of the array for which the  
block  
returns true, i.e the first element which is greater than item; then  
anArray.index will return the index of the item. (This returns nil if no  
element in the array is greater than item)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 2, 2007, 11:01am UTC](https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765/3 "2007-06-02T11:01:15Z")

</div>

On 02.06.2007 09:30, Josselin wrote:

> 5.0 index 2  
> can it be writtent in just one line or a loop is unavoidable ?  
> Yes, it can. Just use Enumerator.

irb(main):001:0\> anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]  
=\> [0.0, 2.0, 5.0, 10.0, 15.0, 25.0]

irb(main):007:0\> anArray.to\_enum(:each\_with\_index).find {|n,i| n \> 30}  
=\> nil  
irb(main):008:0\> anArray.to\_enum(:each\_with\_index).find {|n,i| n \> 10}  
=\> [15.0, 4]  
irb(main):009:0\> anArray.to\_enum(:each\_with\_index).find {|n,i| n \>  
-2.23}  
=\> [0.0, 0]  
irb(main):010:0\> val, idx = anArray.to\_enum(:each\_with\_index).find  
{|n,i| n \> -2.23}  
=\> [0.0, 0]  
irb(main):011:0\> val  
=\> 0.0  
irb(main):012:0\> idx  
=\> 0

This also has the advantage of having to traverse the array just once  
vs. the solution Stefano presented.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 2, 2007, 11:26am UTC](https://www.ruby-forum.com/t/array-index-of-first-bigger-number/105765/4 "2007-06-02T11:26:18Z")

</div>

On 2007-06-02 10:59:03 +0200, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> > anArray = [0.0, 2.0, 5.0, 10.0, 15.0, 25.0] first\_bigger\_element =
> 
> =\> [15.0, 4]  
> This also has the advantage of having to traverse the array just once  
> vs. the solution Stefano presented.
> 
> Kind regards
> 
> robert

thanks, Robert  
that’s what I was trying to avoid… I was close to Stefano’s solution
