Forum: Ruby on Rails Finding highest value in a Hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arch S. (Guest)
on 2007-04-04 05:19
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?
Jeff C. (Guest)
on 2007-04-04 07:10
(Received via mailing list)
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 S. <removed_email_address@domain.invalid>
Arch S. (Guest)
on 2007-04-04 19:55
Jeff C. 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 S. <removed_email_address@domain.invalid>

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.
Arch S. (Guest)
on 2007-04-04 19:58
> 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.
John M. (Guest)
on 2007-04-04 20:13
Arch S. wrote:
> Jeff C. 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 S. <removed_email_address@domain.invalid>
>
> 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
Arch S. (Guest)
on 2007-04-05 07:24
Thanks Jeff and Jim - everything is working as planned now.
This topic is locked and can not be replied to.