Re: [Rails] How to remove a record if it is duplicate end sum values :val
I'm sorry if it made no sense to my sentence. The next will be clearer.
On Fri, Aug 3, 2012 at 9:14 AM, Colin Law <clanlaw@googlemail.com> wrote:
On 3 August 2012 13:00, Esmerino Jr <jose.esmerino@gmail.com> wrote:I think you need to make it more clear what you are trying to achieve.
> Michael Pavling, 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 ?
The sentence " But your solution only removes duplication and the sum
of :val?" does not make sense.
Colin
>
> 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 Fri, Aug 3, 2012 at 4:58 AM, Tom Meinlschmidt <tomas@meinlschmidt.com>
> wrote:
>>
>> 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 Costa <hakagura@gmail.com> wrote:
>>
>> > 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.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Ruby on Rails: Talk" group.
>> > To post to this group, send email to rubyonrails-talk@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > rubyonrails-talk+unsubscribe@googlegroups.com.
>> > 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
>>
>> www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
>>
>> ===============================================================================
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To post to this group, send email to rubyonrails-talk@googlegroups.com.
>> To unsubscribe from this group, send email to
>> rubyonrails-talk+unsubscribe@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home