I trying to get a new hashes of array, it will list every :name in the hashes of array(e) which was provided only once, and the value should be the total score which should be the sum of scores under same :name in the array(e).
e = [
{:name => "Oyster", :score => 1000},
{:name => "Tiger", :score => 2000},
{:name => "Frog", :score => 2300},
{:name => "Frog", :score => 1220},
{:name => "Tiger", :score => 200},
{:name => "Frog", :score => 1000},
{:name => "Oyster", :score => 2000},
{:name => "Frog", :score => 300},
{:name => "Monkey", :score => 500},
{:name => "Oyster", :score => 3300}
]
I searched google for 「hash array ruby」, and ruby's documentation but haven't got any very useful information yet.
I tried to solve this problem by doing the following:
f = e.map{|t|t[:name]}.uniq
n = Hash[f.map{|v|[v,0]}]
result = n.map{|key, value|e.map{|t| if key == t[:name]; n[key] += t[:score].to_i end}}
but the result is
[[1000, nil, nil, nil, nil, nil, 3000, nil, nil, 6300], [nil, 2000, nil, nil, 2200, nil, nil, nil, nil, nil], [nil, nil, 2300, 3520, nil, 4520, nil, 4820, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, 500, nil]]
it's totally different from my expectation. if we want to reach the very neat result what should we do.