Displaying data

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

2008/5/20 Tim W. [email protected]:

@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

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.

Please do not top post.

2008/5/20 Tim W. [email protected]:

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

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 :frowning: 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’, 25) do |smtp|
smtp.open_message_stream(‘[email protected]’,
[‘[email protected]’]) do |f|
f.puts ‘From: [email protected]
f.puts ‘To: [email protected]
f.puts ‘Subject: test message’
f.puts
#f.puts mailinfo
f.puts ‘This is a test message.’
end
end