From: Tim Wolak Date: 2008-04-24T22:11:27+09:00 Subject: Hash keys --B_3291869479_317321 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: quoted-printable Morning, I am working on some code to gather accounts and their balances from a text file and have a question about adding that data to a hash. While checking to see if the account number is a key in the existing hash using has_key?(key), can=B9t I use the variable that has the account number in it= or do I have to use the actual account number? When I run my code to check it I get undefined method `has_key?' for nil:NilClass. I ran a test using just the account number in irb and it works but I would think you could use a variable to iterate of my list of account numbers.... Thanks in advance class Info attr_reader :acct, :money =20=20 def initialize(filename) @acct =3D File.new(filename, "r") end f =3D Info.new("Balances20080415.txt") act =3D f.acct act.each do |list| #covert me to a string futbal =3D list.to_s #Pull accounts office =3D futbal[21..23] if office =3D=3D "RPT" next else=20=20 acctnum =3D futbal[24..28] lv =3D futbal[217..230] lvind =3D futbal[215..215] lvadd =3D lvind+lv lvadd.to_f/100 end #if Negitave vlaues if lvind =3D=3D "-" lvnegfloat =3D lv.to_f/100 #print acctnum," ",lvind, lvnegfloat, "\n" #puts lvadd sktylist =3D Hash.new("") #if(sktylist.has_key?(23)) # sktylist =3D { acctnum =3D> lvind,lvnegfloat } =20=20=20=20=20=20=20=20=20=20=20=20=20 sktylist[acctnum] =3D lvind,lvnegfloat sktylist.each { |key, value| puts "#{key} equals #{value}" } #else Positive Values =20=20=20=20=20=20=20=20=20=20=20=20=20=20 elsif sktylist.has_key?('acctnum') puts "found #{key}second key" #lvposflt =3D lv.to_f/100 =20=20=20=20=20=20=20=20=20=20=20=20=20=20 #print acctnum, " ", lvposflt, "\n" end end =20=20=20=20=20=20 end --=20 This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. --B_3291869479_317321--