From: Gordon Thiesfeld Date: 2008-04-25T00:45:33+09:00 Subject: Re: twolak@sktydev.com - Found word(s) list error in the Text body - Re: Hash keys On Thu, Apr 24, 2008 at 9:40 AM, Tim Wolak wrote: > The first if does return a hash with the keys and values so I know that > works. The first one is for any negative values as it is only indicated by > a 0 or a - in position 215 of the string. The second if is for the positive > values, so I'm guessing that when the script is processing the lines and has > a positive value its not skipping the negative if and going to the elsif to > process and store the info in the hash. > > Basically what I'm trying to do here is gather the account balances and > account numbers and store them in a hash and if there are accounts with the > same number to store their balances as an array in the hash value to add > them later when I iterate over the hash. > > If there is a better way to do this that I'm not thinking of please share > with me so I can better my ruby knowledge. > > Thanks! > Tim Here are some ideas. You create the Hash using a block, this will give you a default value for newly created hash pairs. You iterate over the file line by line, you skip the lines that have an office code of RPT. Otherwise, you pull out the account number and the balance. You add the balance in to the hash pair for that account number. If no values have been put in yet, you'll add it to 0. Otherwise, you'll add it to the previous value. There is no need to handle negative numbers in some special fashion, Ruby will know if it's a negative number when it sees the '-' at the beginning of the string. I'm assuming you don't want whatever is at position 216 to be part of your balance, so you concatenate the character at 215 to the rest of the balance in positions 217 to 230. You now have a string representation of your balance, you call to_f on that and it will be a positive or negative number. act = File.new("Balances20080415.txt", 'r') # The default for a newly created value is 0 act.each do |list| # list already is a string, no need to # do this # futbal = list.to_s #Pull accounts office = list[21..23] unless office == "RPT" acctnum = list[24..28] lvind = list[215,1] lvnum = list[217..230] lv = (lvind+lvnum).to_f/100 sktylist[acctnum] += lv end end p sktylist