# Find display value in array

**URL:** <https://www.ruby-forum.com/t/find-display-value-in-array/108995>\
**Category:** Ruby\
**Created:** [July 10, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995 "2007-07-10T14:10:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/1 "2007-07-10T14:10:28Z")

</div>

Hi,

I have a array that looks like this  
Type = [[“key”,“value”],[“key…”,“value…”],["…","…"]]

Now i want to the the display (value) when i have the key, is this a OK  
way to do it? Or is there a better way?

Type.select{|key,disp| key == “H” }.collect{|key,value|value}.to\_s

---

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/2 "2007-07-10T14:21:27Z")

</div>

On Jul 10, 2007, at 7:10 AM, Jon S. wrote:

> Hi,
> 
> I have a array that looks like this  
> Type = [[“key”,“value”],[“key…”,“value…”],["…","…"]]
> 
> Now i want to the the display (value) when i have the key, is this  
> a OK  
> way to do it? Or is there a better way?

Yes. You want the assoc() method:

> > assoc\_array = [%w[key1 value1], %w[key2 value2], %w[key3 value3]]  
> > =\> [[“key1”, “value1”], [“key2”, “value2”], [“key3”, “value3”]]
> 
> > assoc\_array.assoc(“key2”).last  
> > =\> “value2”

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/3 "2007-07-10T14:45:31Z")

</div>

> Type = [[“key”,“value”],[“key…”,“value…”],["…","…"]]
> 
> Now i want to the the display (value) when i have the key, is this a OK  
> way to do it? Or is there a better way?
> 
> Type.select{|key,disp| key == “H” }.collect{|key,value|value}.to\_s

Is this what you want to do?  
If so, I think iteration is easier than that.  
I think it is also faster but I did not check.

arr = [[“A”,2],[“H”,3],[“H”,4]]  
arr.each {|x| print x[1] if x[0] == “H”}

Harry

---

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/4 "2007-07-10T14:54:45Z")

</div>

Thanks for a good solution!

Now i saw that i had may key on in the last position in the inner array,  
is there someother function that i use insted, so i can search for last  
item? and return the first.

James G. wrote:

> On Jul 10, 2007, at 7:10 AM, Jon S. wrote:
> 
> > Hi,
> > 
> > I have a array that looks like this  
> > Type = [[“key”,“value”],[“key…”,“value…”],["…","…"]]
> > 
> > Now i want to the the display (value) when i have the key, is this  
> > a OK  
> > way to do it? Or is there a better way?
> 
> Yes. You want the assoc() method:
> 
> > > assoc\_array = [%w[key1 value1], %w[key2 value2], %w[key3 value3]]  
> > > =\> [[“key1”, “value1”], [“key2”, “value2”], [“key3”, “value3”]]
> > 
> > > assoc\_array.assoc(“key2”).last  
> > > =\> “value2”
> 
> Hope that helps.
> 
> James Edward G. II

---

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/5 "2007-07-10T16:37:24Z")

</div>

Aha, sweet 😉 Thank you!

Florian G. wrote:

> On Jul 10, 2:54 pm, Jon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Now i saw that i had may key on in the last position in the inner array,  
> > is there someother function that i use insted, so i can search for last  
> > item? and return the first.
> 
> Try rassoc. 🙂

---

<div class="post-metadata">

**Author:** ![stonebreaker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@stonebreaker](https://www.ruby-forum.com/u/stonebreaker)\
**Post date:** [July 10, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/find-display-value-in-array/108995/6 "2007-07-10T15:02:33Z")

</div>

On Jul 10, 2:54 pm, Jon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now i saw that i had may key on in the last position in the inner array,  
> is there someother function that i use insted, so i can search for last  
> item? and return the first.

Try rassoc. 🙂
