From: Tim Wolak Date: 2008-04-24T23:40:01+09:00 Subject: Re: twolak@sktydev.com - Found word(s) list error in the Text body - Re: Hash keys >> class Info >> attr_reader :acct, :money >> >> def initialize(filename) >> @acct = File.new(filename, "r") >> end >> f = Info.new("Balances20080415.txt") >> act = f.acct >> act.each do |list| >> #covert me to a string >> futbal = list.to_s >> #Pull accounts >> office = futbal[21..23] >> if office == "RPT" >> next >> else >> acctnum = futbal[24..28] >> lv = futbal[217..230] >> lvind = futbal[215..215] >> lvadd = lvind+lv >> lvadd.to_f/100 >> end >> #if Negitave vlaues >> if lvind == "-" >> lvnegfloat = lv.to_f/100 >> #print acctnum," ",lvind, lvnegfloat, "\n" >> #puts lvadd >> sktylist = Hash.new("") >> #if(sktylist.has_key?(23)) >> # sktylist = { acctnum => lvind,lvnegfloat } >> >> sktylist[acctnum] = lvind,lvnegfloat >> sktylist.each { |key, value| puts "#{key} equals #{value}" } >> #else Positive Values >> >> elsif sktylist.has_key?('acctnum') >> puts "found #{key}second key" >> #lvposflt = lv.to_f/100 >> >> #print acctnum, " ", lvposflt, "\n" >> end >> end >> >> end > > Your if clauses looked messed up. The no method error doesn't have > anything to do with the key of has_key. You actually have no hash to > speak of. > > T. 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 -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.