From: Sandro Paganotti Date: 2008-05-07T22:21:58+09:00 Subject: Re: changing hash key 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}" } On Wed, May 7, 2008 at 1:04 PM, Tim Wolak wrote: > 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}" > } > -- > Posted via http://www.ruby-forum.com/. > > -- Go outside! The graphics are amazing!