aris
August 3, 2012, 9:14am
1
I have a hash like so:
list=[
{:cod => “0001”, :name => “name1”, :val => 10},
{:cod => “0001”, :name => “name1”, :val => 12},
{:cod => “0002”, :name => “name2”, :val => 13},
{:cod => “0002”, :name => “name2”, :val => 14},
{:cod => “0002”, :name => “name2”, :val => 14},
{:cod => “0004”, :name => “name4”, :val => 16},
{:cod => “0004”, :name => “name4”, :val => 16},
{:cod => “0004”, :name => “name4”, :val => 17},
{:cod => “0005”, :name => “name5”, :val => 17},
{:cod => “0005”, :name => “name5”, :val => 17},
{:cod => “0005”, :name => “name5”, :val => 17},
{:cod => “0006”, :name => “name6”, :val => 110},
{:cod => “0006”, :name => “name6”, :val => 10},
]
What I would like to learn how to do is how to remove a record if it is
duplicate end sum values :val.
On 1 August 2012 14:25, Esmerino C. [email protected] wrote:
I have a hash like so:
That’s not a hash, it’s an array of hashes.
What I would like to learn how to do is how to remove a record if it is
duplicate end sum values :val.
list.uniq!
saw something similar somewhere as a test for job seekers
so don’t know if someone will help you as it’s not so hard to solve
tom
On Aug 1, 2012, at 15:25 , Esmerino C. [email protected] wrote:
{:cod => “0004”, :name => “name4”, :val => 17},
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/SJr0rc22ScgJ .
For more options, visit https://groups.google.com/groups/opt_out .
–
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
Michael P., thanks for reply.
Okay, i agree that is a array of hash : list.class => Array end
list.last.class => Hash.
But your solution only removes duplication and the sum of :val ?
Tom Meinlschmid, thanks for reply.
Not a test for job seekers
Solution:
list.inject(Hash.new(0)) { |hash, el| hash[el[:cod]] += el[:val] ; hash
}
On 3 August 2012 13:00, Esmerino Jr [email protected] wrote:
Michael P., thanks for reply.
Okay, i agree that is a array of hash : list.class => Array end
list.last.class => Hash.
But your solution only removes duplication and the sum of :val ?
I think you need to make it more clear what you are trying to achieve.
The sentence " But your solution only removes duplication and the sum
of :val?" does not make sense.
Colin
I’m sorry if it made no sense to my sentence. The next will be clearer.