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 For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Aug 1, 2012 at 8:35 PM, Esmerino Jr <jose.esmerino-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.try list.uniq kind regards -botp -- 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.
Thanks for replay, but this solution only remove the duplication of values and sum of values duplication? Thanks again. On Fri, Aug 3, 2012 at 8:13 AM, botp <botpena-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Aug 1, 2012 at 8:35 PM, Esmerino Jr <jose.esmerino-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. > > try > > list.uniq > > kind regards -botp > > -- > 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.
On Fri, Aug 3, 2012 at 7:33 PM, Esmerino Jr <jose.esmerino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for replay, but this solution only remove the duplication of values > and sum of values duplication?pls show what output you really want. -- 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.
Botp, thanks for replay, this is solution: list.inject(Hash.new(0)) { |hash, el| hash[el[:cod]] += el[:val] ; hash } I want it out: {"0006"=>120, "0005"=>17, "0004"=>33, "0002"=>27, "0001"=>22} Thanks for replay again. On Fri, Aug 3, 2012 at 8:39 AM, botp <botpena-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Aug 3, 2012 at 7:33 PM, Esmerino Jr <jose.esmerino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Thanks for replay, but this solution only remove the duplication of > values > > and sum of values duplication? > > pls show what output you really want. > > -- > 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.