From: Tim Wolak Date: 2008-05-08T04:57:40+09:00 Subject: Re: hash adding values Siep Korteling wrote: > Craig Demyanovich wrote: >> Looks like you forgot to change this line >> >> sktylist = Hash.new("") >> >> to >> >> sktylist = Hash.new( [] ) >> >> which makes the default value for new elements an empty array instead of >> a >> string. Only then can you use the << operator to push values on to the >> array. >> >> Regards, >> Craig > > You mean the block form: Hash.new{ [] } > Tim, you don't have to do classes. > > f = Info.new("SKTYFutBalances20080415.txt") > @sktylist1 = Hash.new{ [] } #with curly braces > @sktylist2 = Hash.new(0) > f.each do |list| > # parsing stuff here > > > @sktylist1[ @acctnum ] = @sktylist1[ @acctnum ] << value > @sktylist2[ @acctnum ] += value > end > > p @sktylist1 > p @sktylist2 > > regards, > > Siep 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]} -- Posted via http://www.ruby-forum.com/.