T
Tim Wolak
Can Ruby create a hash and have a key and the value be an array?
I am taking account information from a txt file like such:
000123456778900000000-1234567890000000000000000000230000000
I=B9m getting the account number and balance from each line. If the account
numbers are the same I need to add the balances together else move on to the
next. The best way for me to do that would be to store the account number
in a hash key and store the balances from the lines that have the same
account number into an array to be added together later. The code to start
gathering the info is located below. As I am extremely new to Ruby any help
would be appreciated. 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[22..24]
if office =3D=3D "RPT"
next
else=20=20
acctnum =3D futbal[24..28]
end
#Pull Liquidating values
lv =3D futbal[217..231]
#Pull LV Indicator
lvind =3D futbal[215..215]
#if Negitave vlaues
if lvind =3D=3D "-"
lvnegfloat =3D lv.to_f/1000
print acctnum," ",lvind, lvnegfloat, "\n"
#else Positive Values
else
lvposflt =3D lv.to_f/1000
print acctnum, " ", lvposflt, "\n"
end
end
end
--=20
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
I am taking account information from a txt file like such:
000123456778900000000-1234567890000000000000000000230000000
I=B9m getting the account number and balance from each line. If the account
numbers are the same I need to add the balances together else move on to the
next. The best way for me to do that would be to store the account number
in a hash key and store the balances from the lines that have the same
account number into an array to be added together later. The code to start
gathering the info is located below. As I am extremely new to Ruby any help
would be appreciated. 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[22..24]
if office =3D=3D "RPT"
next
else=20=20
acctnum =3D futbal[24..28]
end
#Pull Liquidating values
lv =3D futbal[217..231]
#Pull LV Indicator
lvind =3D futbal[215..215]
#if Negitave vlaues
if lvind =3D=3D "-"
lvnegfloat =3D lv.to_f/1000
print acctnum," ",lvind, lvnegfloat, "\n"
#else Positive Values
else
lvposflt =3D lv.to_f/1000
print acctnum, " ", lvposflt, "\n"
end
end
end
--=20
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.