Esmerino Costa
2012-Aug-01 13:25 UTC
How to remove a record if it is duplicate end sum values :val
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Michael Pavling
2012-Aug-03 07:20 UTC
Re: How to remove a record if it is duplicate end sum values :val
On 1 August 2012 14:25, Esmerino Costa <hakagura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Tom Meinlschmidt
2012-Aug-03 07:58 UTC
Re: How to remove a record if it is duplicate end sum values :val
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Esmerino Jr
2012-Aug-03 12:00 UTC
Re: How to remove a record if it is duplicate end sum values :val
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 ? 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-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org>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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2012-Aug-03 12:14 UTC
Re: How to remove a record if it is duplicate end sum values :val
On 3 August 2012 13:00, Esmerino Jr <jose.esmerino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 ?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> > 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-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> > 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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To unsubscribe from this group, send email to >> > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Esmerino Jr
2012-Aug-03 12:38 UTC
Re: 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-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 3 August 2012 13:00, Esmerino Jr <jose.esmerino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 ? > > 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 > > > > > 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-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org > > > > 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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > To unsubscribe from this group, send email to > >> > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> To unsubscribe from this group, send email to > >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.