From: "Szabolcs Tóth" Date: 2010-11-08T21:36:44+09:00 Subject: Comparing/search struct array 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/.