# Want to search value in nested arrays and return other value

**URL:** <https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269>\
**Category:** Ruby\
**Created:** [December 10, 2008, 12:04am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269 "2008-12-10T00:04:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [December 10, 2008, 12:04am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269/1 "2008-12-10T00:04:59Z")

</div>

Hi.

I have following problem:

VALUES = [[‘value 1’, 1], [‘value 2’, 2], [‘value 3’, 3]]

I need a function that returns ‘value 1’ if I call the function with  
parameter 1.

I tried this:

def value\_name(value)  
VALUES.select {|v| v[1] == 1}[0][0]  
end

so value\_name(1) returns ‘value 1’ but is there something more error  
tolerant? E.g. returning ‘’ if value isn’t present instead of throwing  
an error?

Changing the structure of VALUES is no option, I need exactly this  
structure for options\_for\_select from the Rails framework

Thanks in advance  
Andreas

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [December 10, 2008, 1:42am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269/2 "2008-12-10T01:42:18Z")

</div>

Are you the one defining the data structure?  
Or is it something you just have to use the way it is?

Because in this case, a hashmap would make more sense I believe.

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [December 10, 2008, 2:31am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269/3 "2008-12-10T02:31:40Z")

</div>

On Wed, Dec 10, 2008 at 6:58 AM, Joao S. \> I tried this:

> def value\_name(value)  
> VALUES.select {|v| v[1] == 1}[0][0]  
> end

did you really check that? it will always return “value 1” 😉

> so value\_name(1) returns ‘value 1’ but is there something more error  
> tolerant? E.g. returning ‘’ if value isn’t present instead of throwing an error?

then just create it  
eg,

> def value\_name3(value)  
> VALUES.each{|k,v| return k if v== value}  
> “”  
> end  
> =\> nil  
> value\_name3 1  
> =\> “value 1”  
> value\_name3 2  
> =\> “value 2”  
> value\_name3 3  
> =\> “value 3”  
> value\_name3 4  
> =\> “”  
> value\_name3 “asdf”  
> =\> “”

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [December 10, 2008, 11:24am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269/4 "2008-12-10T11:24:42Z")

</div>

Pierre P. wrote:

> Are you the one defining the data structure?  
> Or is it something you just have to use the way it is?
> 
> Because in this case, a hashmap would make more sense I believe.  
> I need exactly this structure because it is used in options\_for\_select()  
> in Rails Framework, so i can’t change this. A hash is unsorted, and I  
> need this sorted.

botp wrote:

> did you really check that? it will always return “value 1” 😉  
> erm, right 🙂 I meant ‘v[1] == value’ - thats how I tried it out on my  
> computer 🙂

> def value\_name3(value)  
> VALUES.each{|k,v| return k if v== value}  
> “”  
> end  
> Thank you very much, that does the task and works as I wanted to have  
> it.

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [December 10, 2008, 11:48am UTC](https://www.ruby-forum.com/t/want-to-search-value-in-nested-arrays-and-return-other-value/155269/5 "2008-12-10T11:48:28Z")

</div>

Joao S. wrote:

> Hi.
> 
> I have following problem:
> 
> VALUES = [[‘value 1’, 1], [‘value 2’, 2], [‘value 3’, 3]]
> 
> I need a function that returns ‘value 1’ if I call the function with  
> parameter 1.

Try .rassoc(1).first

Regards  
Stefan
