ruby - add up values from 2 arrays based on duplicate values of the other one -


a similar question has been answered here i'd know how can add up/group numbers 1 array based on duplicate values of array.

test_names = ["test1", "test1", "test2", "test3", "test2", "test4", "test4", "test4"] numbers = ["5", "4", "3", "2", "9", "7", "6", "1"] 

the ideal result i'd hash or array with:

{"test1" => 9, "test2" => 12, "test3" => 2, "test4" => 14} 

you can this:

my_hash = hash.new(0) test_names.each_with_index {|name, index| my_hash[name] += numbers[index].to_i} my_hash #=> {"test1"=>9, "test2"=>12, "test3"=>2, "test4"=>14} 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -