Find element in array of hashes

array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.45} # index[2]
{:id=>4, :price =>0.55} # index[3]

I am beginning to adventure in Ruby and I need to find in this array of
hashes the id with the value of 3 and get its price. What would be the
easiest way to do it?

Thanks

Hi Rodrigo,

Check out Enumerable#find.

With a hash it’ll look something like this:
hsh.find { |key, value| value[:yourkey] == yourvalue }

That should get you on the right track.

Cheers,
Jason

“3” is not the same as 3

Thanks

I tried your code but I keep getting nil, but I know for sure that the
value “3” is in there.

sql = "SELECT p.id, s.price FROM services s inner join products as p

ON s.product_id = p.id where s.shop_id = 1"
r = ActiveRecord::Base.connection.select_all(sql)
@array = r.find {|k,v| k[:id] == “3”}

if I change it to
@array = r.find {|k,v| v[:id] == “3”}
I get
undefined method `[]’ for nil:NilClass

Rod

Joel, I fixed it, but still get the error. Look:

results = @selectedProductsArray.find {|k,v| v[:id] == 3 }

NoMethodError in ServicesController#list

undefined method `[]’ for nil:NilClass

I know it is an array, but inside the array there are two hashe objects
and
this is driving me nuts as to using the code above. Look the picture
attached:

Rodrigo L. wrote in post #1113851:

I fixed it, but still get the error. Look:

results = @selectedProductsArray.find {|k,v| v[:id] == 3 }

NoMethodError in ServicesController#list

undefined method `[]’ for nil:NilClass

An array is a sequence of elements, not key-value pairs:

results = @selectedProductsArray.find {|e| e[:id] == 3 }

On Fri, Jun 28, 2013 at 7:21 PM, Rodrigo L.
[email protected]wrote:

array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.25} # index[2]

I am beginning to adventure in Ruby and I need to find in this array of
hashes the id with the value of 3 and get its price. What would be the
easiest way to do it?

There is no Array:

ruby <<XXXX
array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.25} # index[2]

p array
XXXX
{:id=>1, :price=>0.25}

Cheers

robert

How about this?

results = @selectedProductsArray.find {|hash| hash[:id] == 3 }

I am sorry, I did not understand what you tried to convey. To me it is
clear that is an array of hashes. I just don’t get your example…

On 6/28/2013 5:58 PM, Rodrigo L. wrote:

I am sorry, I did not understand what you tried to convey. To me it is
clear that is an array of hashes. I just don’t get your example…

He means your example code did not produce and array. Consider the
following irb session:

irb(main):003:0> array = {:id => 1, :price => 0.25}
=> {:id=>1, :price=>0.25}
irb(main):005:0> {:id => 2, :price => 0.35}
=> {:id=>2, :price=>0.35}
irb(main):006:0> {:id => 3, :price => 0.25}
=> {:id=>3, :price=>0.25}
irb(main):007:0> array
=> {:id=>1, :price=>0.25}
irb(main):008:0> array.class
=> Hash

That’s because you need to put your array in a comma separated list of
values inside []:
irb(main):026:0> array = [{ :id => 1, :price => 0.25 },
irb(main):027:1* { :id => 2, :price => 0.35 },
irb(main):028:1* { :id => 3, :price => 0.25 }]
=> [{:id=>1, :price=>0.25}, {:id=>2, :price=>0.35}, {:id=>3,
:price=>0.25}]

Now that we have an array, we can find the element with the id of 3 like
so:
irb(main):029:0> item = array.find {|hash| hash[:id] == 3 }
=> {:id=>3, :price=>0.25}

and get it’s price:
irb(main):030:0> item[:price]
=> 0.25

Hopefully that helps.

Walton

Joel,
I tried your suggestion and I get “nil” as a result. It is a just weird
behavior!!!

results = @selectedProductsArray.find {|hash| hash[:id] == 3 }

results = nil

Walton,

I got it to work now! This was my fault.

I did not post the the sample array correctly. It should be like this:

array = {:id=>1, :price =>0.25}, # index[0]
:id=>2, :price =>0.35}, # index[1]
:id=>3, :price =>0.45}, # index[2]
:id=>4, :price =>0.55} # index[3]

thank you for all you guys

Am 29.06.2013 03:43, schrieb Rodrigo L.:

thank you for all you guys

No, that’s still not an array of hashes, that’s a couple of
syntax errors. You are still forgetting ‘[’, ‘]’ and some ‘{’ …

This is an array of hashes:

array = [
{:id => 1, :price => 0.25}, # index[0]
{:id => 2, :price => 0.35}, # index[1]
{:id => 3, :price => 0.45}, # index[2]
{:id => 4, :price => 0.55} # index[3]
]

On Sat, Jun 29, 2013 at 6:05 PM, Rodrigo L.
[email protected]wrote:

Thanks,

Just to conclude my approach.

hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }

This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }

Nobody will be able to understand your conclusion in absence of the real
code you used to create the Array - if you created an array.

Good luck!

robert

Thanks,

Just to conclude my approach.

hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }

This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }

Rod

It’s worrying that so many people who put their code problems on here
seem to manually type what they’re looking at, instead of using
copy-paste.

Am 29.06.2013 18:05, schrieb Rodrigo L.:

Thanks,

Just to conclude my approach.

hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }

This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }

Of course, when your actual hashes use strings as keys,
and not symbols like in the example you gave earlier.

Regards,
Marcus

On Sat, Jun 29, 2013 at 8:18 PM, Joel P. [email protected]
wrote:

It’s worrying that so many people who put their code problems on here
seem to manually type what they’re looking at, instead of using
copy-paste.

Well, maybe someone told them that c&p is bad in programming…

:wink:

robert

Taking DRY to the extreme…

Sent from my phone, so excuse the typos.