# Changing hash key

**URL:** <https://www.ruby-forum.com/t/changing-hash-key/136933>\
**Category:** Ruby\
**Created:** [May 7, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/changing-hash-key/136933 "2008-05-07T15:04:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 7, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/1 "2008-05-07T15:04:25Z")

</div>

I am working with a script to record accounts and their balances. I  
need to check the key in a hash and if the account number is already  
there just add the blanace to the value. So far this is what I have and  
its not quite doing what I need. Is my test not right? I get all the  
account numbers and their balances instead of a list with the accounts  
and balances without the duplicate account numbers.

Thanks,  
Tim

sktylist = Hash.new("")  
if sktylist.has\_key?(’@acctnum’)  
sktylist.merge!(’@acctnum’ =\> ‘value’)  
else  
sktylist[@acctnum] = [value]  
end  
sktylist.each { |key, value| puts “#{key} equals #{value}”  
}

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 7, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/2 "2008-05-07T15:24:49Z")

</div>

This is not working because you’re using a string called ‘@acctnum’  
instead of the content of the variable @acctnum inside both has\_key? and  
merge functions

First remove ’ signs from that functions, next I suggest you to use  
symbols instead  
of variables for keys (@acctnum.to\_sym instead of @acctnum)

Bye

sktylist = Hash.new("")  
if sktylist.has\_key?(’@acctnum’)  
sktylist.merge!(’@acctnum’ =\> ‘value’)  
else  
sktylist[@acctnum] = [value]  
end  
sktylist.each { |key, value| puts “#{key} equals #{value}”  
}

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 7, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/3 "2008-05-07T19:00:37Z")

</div>

Sandro P. wrote:

> This is not working because you’re using a string called ‘@acctnum’  
> instead of the content of the variable @acctnum inside both has\_key? and  
> merge functions
> 
> First remove ’ signs from that functions, next I suggest you to use  
> symbols instead  
> of variables for keys (@acctnum.to\_sym instead of @acctnum)
> 
> Bye
> 
> sktylist = Hash.new("")  
> if sktylist.has\_key?(’@acctnum’)  
> sktylist.merge!(’@acctnum’ =\> ‘value’)  
> else  
> sktylist[@acctnum] = [value]  
> end  
> sktylist.each { |key, value| puts “#{key} equals #{value}”  
> }

I’ve created the hash and can store all keys and values however I’m  
trying += to add the balances which is value in the hash. So far the  
accounts that have the same number are not being consolidated into one  
key, value… Can someone tell me what I’m doing wrong with this?

Thanks,  
Tim

sktylist = Hash.new("")  
sktylist[@acctnum] += [value]  
p sktylist

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 8, 2008, 10:23am UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/4 "2008-05-08T10:23:19Z")

</div>

On Wed, May 7, 2008 at 7:00 PM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> ## trying += to add the balances which is value in the hash. So far the

Given the limited amount of information I have I would guess that you  
might doing this

TypeError: can’t convert Array into String

wrong.

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 8, 2008, 11:43am UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/5 "2008-05-08T11:43:45Z")

</div>

How about:

sktylist = Hash.new  
sktylist.[@acctnum] ||= 0  
sktylist.[@acctnum] += value

by  
TheR

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 8, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/changing-hash-key/136933/6 "2008-05-08T16:58:16Z")

</div>

Damjan R. wrote:

> Â Â sktylist = Hash.new  
> Â Â sktylist.[@acctnum] ||= 0

sktylist = Hash.new(0)
