[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash keys

Tim Wolak

4/24/2008 1:11:00 PM

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.


1 Answer

Trans

4/24/2008 2:29:00 PM

0



On Apr 24, 9:11 am, Tim Wolak <two...@sktydev.com> wrote:
> Morning,
>
> I am working on some code to gather accounts and their balances from a tex=
t
> 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 i=
t 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
>
> 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
> 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 }
>
> sktylist[acctnum] =3D lvind,lvnegfloat
> sktylist.each { |key, value| puts "#{key} equals #{value}" }=

> #else Positive Values
>
> elsif sktylist.has_key?('acctnum')
> puts "found #{key}second key"
> #lvposflt =3D 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.