From: Siep Korteling Date: 2008-05-08T05:44:58+09:00 Subject: Re: hash adding values Tim Wolak wrote: > Siep Korteling wrote: > Siep, > > I worked with your code and its giving me the same thing I had before, > below is the output of the script. What I need to do is for example is > the account number 740 is to store it as key 740 and the value is the > sum of the balances listed. So where there is the account number listed > more than once it uses that account number and balance and moves on to > the next line and if that account number is not in the has it puts a new > account number with its balance. Hope this help make a little more > sense of what I'm trying to do. > > {"700"=>[2876.86]} > {"701"=>[654.18]} > {"702"=>[47.74]} > {"705"=>[-22.26]} > {"707"=>[-120.0]} > {"708"=>[502.63]} > {"711"=>[394.13]} > {"712"=>[210.51]} > {"740"=>[7334.36]} > {"740"=>[-12430.14]} > {"740"=>[0.0]} > {"741"=>[14740.47]} > {"741"=>[44919.21]} > {"741"=>[-1009.83]} > {"742"=>[-385.35]} > {"744"=>[-2994.75]} > {"745"=>[-152.86]} You're creating a different hash for every line. You probably want one hash, looking like this: {700=>2876.86, 701=>654.18, ...} I guess you have a Hash.new somewhere in your do-end block. (it should be before the block) Also you are probably printing the hash inside the block. Try it after the block. @sktylist1 = Hash.new{[]} @sktylist2 = Hash.new(0) DATA.each do|line| temp = line.chomp.split(",") acctnum = temp[0] value = temp[1].to_f @sktylist1[acctnum] = @sktylist1[acctnum] < -- Posted via http://www.ruby-forum.com/.