# Array of hashes (finding value)

**URL:** <https://rubytalk.org/t/array-of-hashes-finding-value/47708>\
**Category:** ruby-talk\
**Created:** [14 July 2008 21:22 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708 "2008-07-14T21:22:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nate\_Leavitt](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nate\_Leavitt](https://rubytalk.org/u/Nate_Leavitt)\
**Post date:** [14 July 2008 21:22 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/1 "2008-07-14T21:22:23Z")

</div>

I'm just looking for other possible solutions. Sorry, this is kinda a  
noob question.

Currently I loop through the array and check the value of each hash key  
to find values. Is that the only and/or best way to find matches?

example:

@invoices = [{:id =\> 1, :first\_name =\> 'nate'}, {:id =\> 2, :fist\_name =\>  
'greg'}, {:id =\> 3, :first\_name =\>}]

@invoices.each do |invoice|  
&nbsp;&nbsp;if invoice[:id] == params[:contact\_id]  
&nbsp;&nbsp;&nbsp;&nbsp;bla... bla...  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Glen\_Holcomb](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Glen\_Holcomb](https://rubytalk.org/u/Glen_Holcomb)\
**Post date:** [14 July 2008 21:32 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/2 "2008-07-14T21:32:05Z")

</div>

With the construct you have there it would be faster to just store the names  
in an array at the index corresponding to :id. Although if this is coming  
out of Rails that probably isn't a viable option. If you are stuck with  
that construct then what you have should work fine. There are probably  
faster/more efficient ways though.

> **···**
>
> On Mon, Jul 14, 2008 at 3:22 PM, Nate Leavitt \<nateleavitt@gmail.com\> wrote:
> 
> > I'm just looking for other possible solutions. Sorry, this is kinda a  
> > noob question.
> > 
> > Currently I loop through the array and check the value of each hash key  
> > to find values. Is that the only and/or best way to find matches?
> > 
> > example:
> > 
> > @invoices = [{:id =\> 1, :first\_name =\> 'nate'}, {:id =\> 2, :fist\_name =\>  
> > 'greg'}, {:id =\> 3, :first\_name =\>}]
> > 
> > @invoices.each do |invoice|  
> > if invoice[:id] == params[:contact\_id]  
> > &nbsp;&nbsp;&nbsp;bla... bla...  
> > end  
> > end  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can't hear a word you're saying."
> 
> -Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Nate\_Leavitt](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nate\_Leavitt](https://rubytalk.org/u/Nate_Leavitt)\
**Post date:** [14 July 2008 21:37 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/3 "2008-07-14T21:37:34Z")

</div>

Glen Holcomb wrote:

> **···**
>
> > On Mon, Jul 14, 2008 at 3:22 PM, Nate Leavitt \<nateleavitt@gmail.com\> \> wrote:
> > 
> > > @invoices.each do |invoice|  
> > > if invoice[:id] == params[:contact\_id]  
> > > &nbsp;&nbsp;&nbsp;bla... bla...  
> > > end  
> > > end  
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > With the construct you have there it would be faster to just store the  
> > names  
> > in an array at the index corresponding to :id. Although if this is  
> > coming  
> > out of Rails that probably isn't a viable option. If you are stuck with  
> > that construct then what you have should work fine. There are probably  
> > faster/more efficient ways though.
> > 
> > --  
> > "Hey brother Christian with your high and mighty errand, Your actions  
> > speak  
> > so loud, I can't hear a word you're saying."
> > 
> > -Greg Graffin (Bad Religion)
> 
> Thanks for the reply. Yes, I am working in rails 🙂 It just seems  
> kinda tedious to keep iterating over all these collections. Just  
> wondering if there is a more efficient way.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [14 July 2008 21:47 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/4 "2008-07-14T21:47:29Z")

</div>

Where did @invoices come from in the first place? If it came from the  
database, you are probably doing things in your loop that you should  
be doing in the sql query (e.g. Invoice.find(params[:contact\_id])  
rather than your if guard in the loop)

martin

> **···**
>
> On Mon, Jul 14, 2008 at 2:37 PM, Nate Leavitt \<nateleavitt@gmail.com\> wrote:
> 
> > > > @invoices.each do |invoice|  
> > > > if invoice[:id] == params[:contact\_id]  
> > > > &nbsp;&nbsp;&nbsp;bla... bla...  
> > > > end
> > 
> > Thanks for the reply. Yes, I am working in rails 🙂 It just seems  
> > kinda tedious to keep iterating over all these collections. Just  
> > wondering if there is a more efficient way.

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [14 July 2008 22:10 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/5 "2008-07-14T22:10:16Z")

</div>

Nate Leavitt wrote:

> Glen Holcomb wrote:

> > in an array at the index corresponding to :id. Although if this is  
> > coming  
> > out of Rails that probably isn't a viable option. If you are stuck with  
> > that construct then what you have should work fine. There are probably  
> > faster/more efficient ways though.

> Thanks for the reply. Yes, I am working in rails 🙂 It just seems  
> kinda tedious to keep iterating over all these collections. Just  
> wondering if there is a more efficient way.

invoice\_set=@invoices.select{|invoice|invoice[:id]==params[:contact\_id]}

invoice\_set.each do |invoice|  
&nbsp;&nbsp;#stuff with invoice  
end

Doubt if it is less work but it feels snappier somehow.

Regards,

Siep

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nate\_Leavitt](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nate\_Leavitt](https://rubytalk.org/u/Nate_Leavitt)\
**Post date:** [14 July 2008 22:43 UTC](https://rubytalk.org/t/array-of-hashes-finding-value/47708/6 "2008-07-14T22:43:26Z")

</div>

Martin DeMello wrote:

> **···**
>
> > On Mon, Jul 14, 2008 at 2:37 PM, Nate Leavitt \<nateleavitt@gmail.com\> \> wrote:
> > 
> > > > > @invoices.each do |invoice|  
> > > > > if invoice[:id] == params[:contact\_id]  
> > > > > &nbsp;&nbsp;&nbsp;bla... bla...  
> > > > > end
> > > 
> > > Thanks for the reply. Yes, I am working in rails 🙂 It just seems  
> > > kinda tedious to keep iterating over all these collections. Just  
> > > wondering if there is a more efficient way.
> > 
> > Where did @invoices come from in the first place? If it came from the  
> > database, you are probably doing things in your loop that you should  
> > be doing in the sql query (e.g. Invoice.find(params[:contact\_id])  
> > rather than your if guard in the loop)
> > 
> > martin
> 
> Yeah... sorry. I actually get the collection through an xml-rpc call 🙂  
> which returns the array of hashes.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
