I have a hash where the values are numbers. After I build the hash I want to get the key back that has the largest number as a value. So for example: I build the hash and add to the values - results = Hash.new(0) score = find_score score.points.each do |point| result_score = ResultScore.find(:first, point) if results.has_key?(result_score.result_id) results[result_score.result_id] =+ result_score_points else results[result_score.result_id] = result_score_points end end Now what I want to do is get the key back out of the hash ''results'' with the highest value. I thought about inverting the hash and then sorting the keys. Does ruby sort integers as keys? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can use the max method on hash objects, but it''s usage is a little odd. Let''s say your hash is h. This returns the key for the highest value: h.max { |a,b| a.last <=> b.last }.first Looks crazy, I know, but it should work. If you want the gory details of what it''s doing, just let me know. :-) Jeff softiesonrails.com On Apr 3, 8:19 pm, Arch Stanton <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a hash where the values are numbers. After I build the hash I > want to get the key back that has the largest number as a value. > > So for example: > > I build the hash and add to the values - > > results = Hash.new(0) > score = find_score > score.points.each do |point| > result_score = ResultScore.find(:first, point) > if results.has_key?(result_score.result_id) > results[result_score.result_id] =+ result_score_points > else > results[result_score.result_id] = result_score_points > end > end > > Now what I want to do is get the key back out of the hash ''results'' with > the highest value. > > I thought about inverting the hash and then sorting the keys. Does ruby > sort integers as keys? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeff Cohen wrote:> You can use the max method on hash objects, but it''s usage is a little > odd. > > Let''s say your hash is h. This returns the key for the highest value: > > h.max { |a,b| a.last <=> b.last }.first > > Looks crazy, I know, but it should work. If you want the gory details > of what it''s doing, just let me know. :-) > > Jeff > softiesonrails.com > > On Apr 3, 8:19 pm, Arch Stanton <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Cool - that worked. It looks to me like it''s a short cut for defining a Comparator? I did find a bug in my logic though . . . If the key already exists, then I want to add to the value. Currently that is not happening, but rather the value is being replaced: 1 if results.has_key?(result_score.result_id) 2 results[result_score.result_id] =+ result_score_points 3 else 4 results[result_score.result_id] = result_score_points 5 end See line 2. I''m obviously not using the Ruby syntax correctly. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I did find a bug in my logic though . . . > > If the key already exists, then I want to add to the value. Currently > that is not happening, but rather the value is being replaced: > > 1 if results.has_key?(result_score.result_id) > 2 results[result_score.result_id] =+ result_score_points > 3 else > 4 results[result_score.result_id] = result_score_points > 5 end > > See line 2. I''m obviously not using the Ruby syntax correctly.I replace line 2 with results[result_score.result_id] = results[result_score.result_id] + result_score.points This works, but isn''t elegant. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Arch Stanton wrote:> Jeff Cohen wrote: >> You can use the max method on hash objects, but it''s usage is a little >> odd. >> >> Let''s say your hash is h. This returns the key for the highest value: >> >> h.max { |a,b| a.last <=> b.last }.first >> >> Looks crazy, I know, but it should work. If you want the gory details >> of what it''s doing, just let me know. :-) >> >> Jeff >> softiesonrails.com >> >> On Apr 3, 8:19 pm, Arch Stanton <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > Cool - that worked. It looks to me like it''s a short cut for defining a > Comparator? > > I did find a bug in my logic though . . . > > If the key already exists, then I want to add to the value. Currently > that is not happening, but rather the value is being replaced: > > 1 if results.has_key?(result_score.result_id) > 2 results[result_score.result_id] =+ result_score_points > 3 else > 4 results[result_score.result_id] = result_score_points > 5 end > > See line 2. I''m obviously not using the Ruby syntax correctly.The operator your looking for is written "+=" as opposed to your "=+". I have a horrible time remembering which way around the "fancy" assignment operators go so I have learned to check them when I bug occurs. JFM -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Jeff and Jim - everything is working as planned now. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---