4

I've got an array of hashes and would like to sum up selected values. I know how to sum all of them or one of them but not how to select more than one key.

i.e.:

[{"a"=>5, "b"=>10, "active"=>"yes"}, {"a"=>5, "b"=>10, "active"=>"no"}, {"a"=>5, "b"=>10, "action"=>"yes"}] 

To sum all of them I using:

t = h.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}}
=> {"a"=>15, "b"=>30, "active"=>"yesnoyes"} # I do not want 'active'

To sum one key, I do:

h.map{|x| x['a']}.reduce(:+)
=> 15 

How do I go about summing up values for keys 'a' and 'b'?

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
Digger
  • 67
  • 3
  • 8

3 Answers3

7

You can use values_at:

hs = [{:a => 1, :b => 2, :c => ""}, {:a => 2, :b => 4, :c => ""}]
keys = [:a, :b]
hs.map { |h| h.values_at(*keys) }.inject { |a, v| a.zip(v).map { |xy| xy.compact.sum }}
# => [3, 6]

If all required keys have values it will be shorter:

hs.map { |h| h.values_at(*keys) }.inject { |a, v| a.zip(v).map(&:sum) }
# => [3, 6]

If you want Hash back:

Hash[keys.zip(hs.map { |h| h.values_at(*keys) }.inject{ |a, v| a.zip(v).map(&:sum) })]
# => {:a => 3, :b => 6}
Victor Moroz
  • 9,167
  • 1
  • 19
  • 23
  • 1
    Thanks @VictorMoroz, your Hash example worked like a charm. Would you be able to explain what you are doing there? Thanks again, – Digger Sep 25 '13 at 23:06
3

I'd do something like this:

a.map { |h| h.values_at("a", "b") }.transpose.map { |v| v.inject(:+) }
#=> [15, 30]

Step by step:

a.map { |h| h.values_at("a", "b") }   #=> [[5, 10], [5, 10], [5, 10]]
 .transpose                           #=> [[5, 5, 5], [10, 10, 10]]
 .map { |v| v.inject(:+) }            #=> [15, 30]
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • 1
    Thanks @Stefan for your comment. How can I add the key to the returned array. I'm expecting it to return: {'a'=>15, 'b'=>30} – Digger Sep 25 '13 at 22:56
  • `Hash[keys.zip(values)]`, e.g. `Hash[["a", "b"].zip([15, 30])]` – Stefan Sep 26 '13 at 06:57
2

How is this ?

h = [{"a"=>5, "b"=>10, "active"=>"yes"}, {"a"=>5, "b"=>10, "active"=>"no"}, {"a"=>5, "b"=>10, "action"=>"yes"}]
p h.map{|e| e.reject{|k,v| %w(active action).include? k } }.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}}
# >> {"a"=>15, "b"=>30}
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317