# N00b question

**URL:** <https://www.ruby-forum.com/t/n00b-question/138772>\
**Category:** Ruby\
**Created:** [May 27, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/n00b-question/138772 "2008-05-27T16:52:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 27, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/n00b-question/138772/1 "2008-05-27T16:52:03Z")

</div>

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.new(:account\_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

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 27, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/n00b-question/138772/2 "2008-05-27T20:37:40Z")

</div>

On Tue, May 27, 2008 at 9:51 AM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @acct.each do |list|
> 
> a = post.future\_data  
> 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

Tim, are you trying to do something along the lines of this? (spacing  
is irrelevant since you specify that by column number, I’m using  
letters for account names instead of your numbers, but it doesn’t  
matter)…

post.txt:  
a 1  
b - 2  
a - 4  
b 8  
RPT1 2  
RPT  
g - 1

prev.txt:  
a 2  
b - 2  
a 0  
b 7  
z 9  
RPT1 2

run\_report.rb:  
post = Hash.new(0)  
IO.foreach(“post.txt”) do |line|  
post[line[3…3]] += line[5…7].to\_i.to\_f unless line[0…2] == “RPT”  
end

prev = Hash.new(0)  
IO.foreach(“prev.txt”) do |line|  
prev[line[3…3]] += line[5…7].to\_i.to\_f unless line[0…2] == “RPT”  
end

report = {}

(post.keys | prev.keys).each do |k|  
report[k] = post[k], prev[k], post[k] - prev[k]  
end

p report

Now, this I would “pretty” up significantly, but I’m just trying to  
get an idea of what your after.

Todd

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 27, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/n00b-question/138772/3 "2008-05-27T21:49:59Z")

</div>

On Tue, May 27, 2008 at 1:36 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

…some code that didn’t paste correctly (the spaces).

This is what it is supposed be…

post.txt:  
a 1  
b - 2  
a - 4  
b 8  
RPT1 2  
RPT  
g - 1

prev.txt:  
a 2  
b - 2  
a 0  
b 7  
z 9  
RPT1 2

run\_report.rb:  
post = Hash.new(0)  
IO.foreach(“post.txt”) do |line|  
post[line[3…3]] += line[5…7].to\_i.to\_f unless line[0…2] == “RPT”  
end

prev = Hash.new(0)  
IO.foreach(“prev.txt”) do |line|  
prev[line[3…3]] += line[5…7].to\_i.to\_f unless line[0…2] == “RPT”  
end

report = {}

(post.keys | prev.keys).each do |k|  
report[k] = post[k], prev[k], post[k] - prev[k]  
end

p report

…and no empty lines in your data files, or you’ll have to ignore  
them, too, in your program.

I figured out what happened. I copied, then pasted back to my editor,  
then copied back again.

Sorry,

Todd

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 28, 2008, 11:20am UTC](https://www.ruby-forum.com/t/n00b-question/138772/4 "2008-05-28T11:20:47Z")

</div>

2008/5/28 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> You are printing key and value of your hash and since the value also  
> contains the account number you see it twice. You probably rather  
> want:
> 
> my\_hash.sort.each |k,v|  
> puts v  
> end

Here’s an alternative

puts my\_hash.values.sort\_by {|v| v.account\_number}

Cheers

robert

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [May 28, 2008, 11:18am UTC](https://www.ruby-forum.com/t/n00b-question/138772/5 "2008-05-28T11:18:59Z")

</div>

2008/5/27 Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.

> g = my\_hash.to\_s.sort  
> puts g

You are printing key and value of your hash and since the value also  
contains the account number you see it twice. You probably rather  
want:

my\_hash.sort.each |k,v|  
puts v  
end

Kind regards

robert
