Comparing/search struct array

  • Thread starter Szabolcs Tóth
  • Start date
S

Szabolcs Tóth

Hi,
I have drafted this tiny script, which works almost(!!) fine except that
every item shows 5x more than it should.

I have one file with the report, where there is no customer name but
customer number is there. I have another file with both names and
numbers. So I put both file into array and try to find the relevant
fields.

if File.exists?("report.output")
puts "We can work..."
else
puts "Copy the 'pascpgen.output'to the same folder, please"
exit
end

# define Class
class Results <
Struct.new:)date, :custo, :invoice, :descr, :type, :money, :ref)
def print_csv_record
date.length==0 ? printf(",") : printf("%s ", date)
custo.length==0 ? printf(",") : printf("%s ", custo)
invoice.length==0 ? printf(",") : printf("%s ", invoice)
descr.length==0 ? printf(",") : printf("%s ", descr)
type.length==0 ? printf(",") : printf("%s ", type)
money.length==0 ? printf(",") : printf("%s ", money)
ref.length==0 ? printf(",") : printf("%s ", ref)
printf("\n")
end
end

class Query <
Struct.new:)cuname, :custo)
def print_csv_record2
cuname.length==0 ? printf(",") : printf("%s", cuname)
# custo.length==0 ? printf(",") : printf("\"%s\",", custo)
# printf("\n")
end
end

# create array
arr = Array.new
arr2 = Array.new


f = File.open("report.output", 'r').each {|line|
if line =~ /MST Revenue/

# read every line into string
str = line.chomp!
str.length

date = str[20, 5]
custo = str[28,6]
invoice = str[48,8]
descr = str[59,28]
type = str[112,4]
money = str[123,11]
ref = str [36,7]

r = Results.new

r.date = date
r.custo = custo
r.invoice = invoice
r.descr = descr
r.type = type
r.money = money
r.ref = ref
arr.push(r)
end
}


f2 = File.open("customers.txt", 'r').each {|line|
# read every line into string

str = line.chomp!
if str != nil
str.length

cuname = str[3, 21]
custo = str[27,6]

q = Query.new

q.cuname = cuname
q.custo = custo
arr2.push(q)
end
}



arr.each { |p| p[:custo]
arr2.each { |q| q[:custo]
if p[:custo] == q[:custo]

q.print_csv_record2
p.print_csv_record

end
}
}

This script finds the matching fields but for some reason it writes 5x
everything. I believe the "arr.each ..." part is wrong. Any idea? Thanks
a lot! Szabolcs
 
S

Szabolcs Toth

Thanks for the comments. I am working on your suggestions.

I figured out that there were repeating lines in the customers.txt that
was the reason why some of the lines were duplicated in the result as
well. So, just modified the customers.txt file read like this:

lastline =""
f2 = File.open("customers.txt", 'r').each {|line|
# read every line into string
str = line.chomp!
if str != nil
str.length

cuname = str[3, 21]
custo = str[27,6]
if lastline != cuname
q = Query.new

q.cuname = cuname
q.custo = custo
arr2.push(q)
end
lastline =cuname
end
}

Now, it is fine. But, more and more I am working on this code, my
feeling is that I started in a very wrong direction and made my life and
the code too complicated.

Thank you again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top