[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unique hash values to totals

Charles L. Snyder

6/11/2006 6:17:00 AM

Hi

I am trying to solve a no doubt trivial problem

given 2 hashes of identical length, with identical keys:

h1 = {'dog'=>'blue, 'car'=>'red', boat=>'blue', 'house'=>'red',
shoe=>'green'}
h2 = {'dog'=>20, 'car' =>60, boat => 90, 'house' =>60, shoe => 70 }

I want to find the sum of the values in the second hash (the numerical
values); based on the values in the first hash:

eg
dogs are blue, and there are 20 dogs
boats are blue, and there are 90 boats
90 + 20 = 110
therefore blue => 110

cars are red, and there are 60 cars
house is red, and there are 60 houses
therefore red => 120

so the resulting hash is something like

h3 = {'blue'=>110, 'red'=>120, ...}

thanks - hopefully there is some easy way to do this I'm overlooking


CLS

3 Answers

Drew

6/11/2006 7:38:00 AM

0


Charles L. Snyder wrote:
> Hi
>
> I am trying to solve a no doubt trivial problem
>
> given 2 hashes of identical length, with identical keys:
>
> h1 = {'dog'=>'blue, 'car'=>'red', boat=>'blue', 'house'=>'red',
> shoe=>'green'}
> h2 = {'dog'=>20, 'car' =>60, boat => 90, 'house' =>60, shoe => 70 }
>
> I want to find the sum of the values in the second hash (the numerical
> values); based on the values in the first hash:
>
> eg
> dogs are blue, and there are 20 dogs
> boats are blue, and there are 90 boats
> 90 + 20 = 110
> therefore blue => 110
>
> cars are red, and there are 60 cars
> house is red, and there are 60 houses
> therefore red => 120
>
> so the resulting hash is something like
>
> h3 = {'blue'=>110, 'red'=>120, ...}
>
> thanks - hopefully there is some easy way to do this I'm overlooking
>
>
> CLS

It seems to me as if you could create your third hash by simply
iterating through the first hash and using the second hash to update
the third hash for each item in the first. I'm somewhat new to Ruby
but I'll try to show what I mean using your example:

h1 = {'dog'=>'blue', 'car'=>'red', 'boat'=>'blue', 'house'=>'red',
'shoe'=>'green'}
h2 = {'dog'=>20, 'car'=>60, 'boat'=>90, 'house'=>60, 'shoe'=>70 }
h3 = {}

h1.each do |key, val|
if !h3.has_key? val
h3[val] = h2[key]
else
h3[val] += h2[key]
end
end

You can test to see if it's working how you want by using something
like:

h3.each {|key, val| puts "#{key}: #{val}"}

I'm pretty sure that's what you meant.

Zeppe

6/11/2006 9:33:00 AM

0

Drew wrote:

> h1 = {'dog'=>'blue', 'car'=>'red', 'boat'=>'blue', 'house'=>'red',
> 'shoe'=>'green'}
> h2 = {'dog'=>20, 'car'=>60, 'boat'=>90, 'house'=>60, 'shoe'=>70 }
> h3 = {}

You can also initialize to zero the third hash, that prevents you to
check if the key already exists or not:

h3 = Hash::new(0) # initialize to zero new keys
h1.each_pair { |key, value| h3[value] += h2[key] }

It should work fine

Bye!

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.or...

Robert Klemme

6/11/2006 9:42:00 AM

0

Zeppe <zeppe@email.it> wrote:
> Drew wrote:
>
>> h1 = {'dog'=>'blue', 'car'=>'red', 'boat'=>'blue', 'house'=>'red',
>> 'shoe'=>'green'}
>> h2 = {'dog'=>20, 'car'=>60, 'boat'=>90, 'house'=>60, 'shoe'=>70 }
>> h3 = {}
>
> You can also initialize to zero the third hash, that prevents you to
> check if the key already exists or not:
>
> h3 = Hash::new(0) # initialize to zero new keys
> h1.each_pair { |key, value| h3[value] += h2[key] }

And the #inject solution with some added safety for values missing from h2:

>> h3 = h1.inject(Hash.new(0)) {|res,(k,v)| res[v] += h2[k] || 0; res}
=> {"green"=>70, "blue"=>110, "red"=>120}

Kind regards

robert