T
Tim Wolak
Can someone explain to me why my script is printing the key to my hash
twice? I need it to print the key then the value on the first iteration
and on the second iteration it should only print the value. I am
getting information from two files, the account numbers and balances and
displaying account number, yesterday balance, today balance and the
difference.
Account = Struct.newaccount_number, :balance_yesterday,
:balance_today) do
def difference
balance_today - balance_yesterday
end
def to_s
"#{account_number}\t#{balance_yesterday}\t#{balance_today}\t#{difference}\n"
end
end
class SktyFut
attr_reader :acct
def initialize(filename)
@acct = File.new(filename, "r")
end
def future_data
@sktylist = Hash.new(0)
@acct.each do |list|
office = list[21..23] # =>
if office == "RPT"
next
else
acctnum = list[24..28]
end
lv = list[217..230]
is_negative = list[215,1] == "-"
value = lv.to_f/100
value = -value if is_negative
# Add vales to hash
@sktylist[acctnum] += value
end
return @sktylist
end
end
Dir.chdir("/Users/twolak")
post = SktyFut.new("FutBalances20080507.txt")
a = post.future_data
#puts a
pre = SktyFut.new("FutBalances20080506.txt")
b = pre.future_data
#puts b
my_hash = Hash.new {|h,k| h[k] = Account.new(k)}
a.each do |acc_nr, balance|
my_hash[acc_nr].balance_yesterday = balance
end
b.each do |key, balance|
my_hash[key].balance_today = balance
end
g = my_hash.to_s.sort
puts g
(the math will be wrong here as I have to take out info for security)
700700 1854.54 5652.05 3237970.2
701701 654.18 654.18 0.0
702702 99.07 99.07 0.0
twice? I need it to print the key then the value on the first iteration
and on the second iteration it should only print the value. I am
getting information from two files, the account numbers and balances and
displaying account number, yesterday balance, today balance and the
difference.
Account = Struct.newaccount_number, :balance_yesterday,
:balance_today) do
def difference
balance_today - balance_yesterday
end
def to_s
"#{account_number}\t#{balance_yesterday}\t#{balance_today}\t#{difference}\n"
end
end
class SktyFut
attr_reader :acct
def initialize(filename)
@acct = File.new(filename, "r")
end
def future_data
@sktylist = Hash.new(0)
@acct.each do |list|
office = list[21..23] # =>
if office == "RPT"
next
else
acctnum = list[24..28]
end
lv = list[217..230]
is_negative = list[215,1] == "-"
value = lv.to_f/100
value = -value if is_negative
# Add vales to hash
@sktylist[acctnum] += value
end
return @sktylist
end
end
Dir.chdir("/Users/twolak")
post = SktyFut.new("FutBalances20080507.txt")
a = post.future_data
#puts a
pre = SktyFut.new("FutBalances20080506.txt")
b = pre.future_data
#puts b
my_hash = Hash.new {|h,k| h[k] = Account.new(k)}
a.each do |acc_nr, balance|
my_hash[acc_nr].balance_yesterday = balance
end
b.each do |key, balance|
my_hash[key].balance_today = balance
end
g = my_hash.to_s.sort
puts g
(the math will be wrong here as I have to take out info for security)
700700 1854.54 5652.05 3237970.2
701701 654.18 654.18 0.0
702702 99.07 99.07 0.0