# Displaying data

**URL:** <https://www.ruby-forum.com/t/displaying-data/138134>\
**Category:** Ruby\
**Created:** [May 20, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/displaying-data/138134 "2008-05-20T14:46:49Z")\
**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 20, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/displaying-data/138134/1 "2008-05-20T14:46:49Z")

</div>

I am getting data from three hashes and am trying to arrange them in  
four columns, however after I print out the first hash there is a new  
line return. How do you remove the new line?

Thanks in advance,  
Tim

def format  
@prevnl.each{ |key, value| puts “#{key}\t #{value}” } + @curnl.each{  
|key, value| puts “#{value}” }  
@finbal.each{ |key, value| puts “#{key} \t #{value}” }  
end

---

<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 20, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/displaying-data/138134/2 "2008-05-20T15:01:14Z")

</div>

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

> @finbal.each{ |key, value| puts “#{key} \t #{value}” }  
> end

This won’t work since the return value of Hash#each is the Hash  
instance itself. I do not know the relationships between your hashes  
but you need to iterate them in parallel (e.g. by using the super set  
of all keys) and print them then.

Having said that you might be better off to use one single Hash only  
and store something as value that contains all the values (Struct or  
OpenStruct come to mind). This is also more efficient memory wise  
because you need to maintain a single hash table vs. three at the  
moment.

Kind regards

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 20, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/displaying-data/138134/3 "2008-05-20T15:13:04Z")

</div>

Thanks Robert! It is actually working but like I said after the first  
has is printed out a new line carriage return is then put in so all my  
data moves down. The performance part of it is ok as this is not a real  
long script, its pretty fast as is. I was just not sure about why when I  
use the concatenation sign it would do a new line.

Thanks,  
Tim

def format  
@prevnl.each{ |key, value| puts “#{key}\t #{value}” } + @curnl.each{  
|key, value| puts “#{value}” }  
@finbal.each{ |key, value| puts “#{key} \t #{value}” }  
end

> This won’t work since the return value of Hash#each is the Hash  
> instance itself. I do not know the relationships between your hashes  
> but you need to iterate them in parallel (e.g. by using the super set  
> of all keys) and print them then.

---

<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 20, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/displaying-data/138134/4 "2008-05-20T15:27:56Z")

</div>

Please do not top post.

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

> Thanks Robert! It is actually working but like I said after the first  
> has is printed out a new line carriage return is then put in so all my  
> data moves down. The performance part of it is ok as this is not a real  
> long script, its pretty fast as is. I was just not sure about why when I  
> use the concatenation sign it would do a new line.

I’m afraid you did not understand how your code works: #puts is the  
method that prints all those newlines. Before @curnl starts to get  
printed all entries of @prevnl have been iterated and printed already.  
The + is meaningless since the result isn’t used for anything.

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 20, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/displaying-data/138134/5 "2008-05-20T15:41:05Z")

</div>

> I’m afraid you did not understand how your code works: #puts is the  
> method that prints all those newlines. Before @curnl starts to get  
> printed all entries of @prevnl have been iterated and printed already.  
> The + is meaningless since the result isn’t used for anything.
> 
> robert

Right I see it now, excuse my n00bism ☹ So what is the correct way to  
print all my data in the four columns that I need?

Below is what I’m working with.

Thanks for the help!  
Tim

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

class Calculate  
attr\_reader :sktyfuta, :sktyfutb  
def initialize(sktyfuta, sktyfutb)  
@sktyfuta = sktyfuta  
@sktyfutb = sktyfutb  
end

def data\_comp  
@sktyfuta.merge(@sktyfutb) { |key, old\_value, new\_value| old\_value -  
new\_value }  
end  
#end  
end

class FinalNum  
attr\_reader :sktynl  
def initialize(sktynl)  
@sktynl = sktynl  
end

def numbers  
@nat = Hash.new(0)  
@sktynl.each do |key, value|  
key.to\_s  
if key \<= “39”  
key = “SKTY” # =\>  
@nat[key] += value  
elsif key \>=“40”  
key = “SKYNY” # =\>  
@nat[key] += value  
end  
end  
return @nat  
end  
end

class Mailer  
attr\_reader :prevnl, :curnl, :finbal  
def initialize(prevnl, curnl, finbal)  
@prevnl = prevnl  
@curnl = curnl  
@finbal = finbal  
end

def format  
@prevnl.each{ |key, value| puts “#{key}\t #{value}” } + @curnl.each{  
|key, value| puts “#{value}” }  
# @curnl.each\_value{ |value| puts value }  
@finbal.each{ |key, value| puts “#{key} \t #{value}” }  
end  
end  
Dir.chdir(“/Users/twolak”)  
post = SktyFut.new(“SKTYFutBal20080507.txt”)  
a = post.future\_data  
postord = a.sort  
#postord.each{|key, value| puts “#{key} A value is #{value}”}  
pre = SktyFut.new(“SKTYFutBal20080506.txt”)  
b = pre.future\_data  
preord = b.sort  
#preord.each{|key, value| puts “#{key} B value is #{value}”}  
data = Calculate.new(a,b)  
iteration = data.data\_comp  
#iteration.sort  
#iteration.each{|key, value| puts “#{key} comp equals #{value}” }  
sktyfinal = FinalNum.new(iteration)  
finumb = sktyfinal.numbers  
mail = Mailer.new(postord, preord, finumb)  
mailinfo = mail.format

require ‘net/smtp’

Net::SMTP.start(‘[mailserver.com](http://mailserver.com)’, 25) do |smtp|  
smtp.open\_message\_stream(‘removed\_email\_address@domain.invalid’,  
[‘removed\_email\_address@domain.invalid’]) do |f|  
f.puts ‘From: removed\_email\_address@domain.invalid’  
f.puts ‘To: removed\_email\_address@domain.invalid’  
f.puts ‘Subject: test message’  
f.puts  
#f.puts mailinfo  
f.puts ‘This is a test message.’  
end  
end
