Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Comparing/search struct array

Reply
Thread Tools

Comparing/search struct array

 
 
Szabolcs Tóth
Guest
Posts: n/a
 
      11-08-2010
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Szabolcs Toth
Guest
Posts: n/a
 
      11-09-2010
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!

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
How to access an Array and a struct within a struct wrapped by Swig ? emmanuel.rivoire@gmail.com Python 3 08-18-2008 03:30 AM
How to define an array of struct nested within a struct? Daniel Rudy C Programming 7 03-31-2006 06:54 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments