Find elment in array of hashes

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?

Thanks

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Fri, Jun 28, 2013 at 1:21 PM, Rodrigo Lueneberg <lists@ruby-forum.com>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?

Thanks

--
Posted via http://www.ruby-forum.com/\.

"3" is not the same as 3

···

--
Posted via http://www.ruby-forum.com/.

How about this?

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Fri, Jun 28, 2013 at 7:21 PM, Rodrigo Lueneberg <lists@ruby-forum.com>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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

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

NoMethodError in ServicesController#list

undefined method `[]' for nil:NilClass

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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..

···

--
Posted via http://www.ruby-forum.com/.

At least as worrying is that people attach jpg(!) screenshots of text.

:slight_smile:

···

Am 29.06.2013 20:18, schrieb Joel Pearson:

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.

--
<https://github.com/stomar/&gt;

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

:wink:

robert

···

On Sat, Jun 29, 2013 at 8:18 PM, Joel Pearson <lists@ruby-forum.com> 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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Rodrigo Lueneberg 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 }

···

--
Posted via http://www.ruby-forum.com/\.

I know it is an array, but inside the array there are two hashes. And
this is driving me nuts. Look the picture attached:

Attachments:
http://www.ruby-forum.com/attachment/8548/array.JPG

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 6/28/2013 5:58 PM, Rodrigo Lueneberg 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..

Taking DRY to the extreme...

···

Sent from my phone, so excuse the typos.
On Jun 30, 2013 6:16 AM, "Robert Klemme" <shortcutter@googlemail.com> wrote:

On Sat, Jun 29, 2013 at 8:18 PM, Joel Pearson <lists@ruby-forum.com>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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

--
Posted via http://www.ruby-forum.com/.

Sorry guys for not Copyind and Pasting. But at the same time I am so
happy that I found a place where people really help each other. Thank
you

···

--
Posted via http://www.ruby-forum.com/.

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]
]

···

Am 29.06.2013 03:43, schrieb Rodrigo Lueneberg:

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

--
<https://github.com/stomar/&gt;

We're always trying to be helpful :slight_smile:

And remember: the more precise the information you give, the better
we can help. Generally, it's good practice to make extensive use
of 'irb' for experimenting/debugging, and often it's also helpful
to include some 'puts myvar', or 'p myvar' statements in the code,
to see what is actually going on.

Have fun hacking Ruby,
Marcus

···

Am 30.06.2013 17:34, schrieb Rodrigo Lueneberg:

Sorry guys for not Copyind and Pasting. But at the same time I am so
happy that I found a place where people really help each other. Thank
you

--
<https://github.com/stomar/&gt;