Comparing/search struct array

Discussion in 'Ruby' started by Szabolcs Tóth, Nov 8, 2010.

  1. 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/.
    Szabolcs Tóth, Nov 8, 2010
    #1
    1. Advertising

  2. 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/.
    Szabolcs Toth, Nov 9, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,345
    Chris Fogelklou
    Apr 20, 2004
  2. Daniel Rudy
    Replies:
    7
    Views:
    439
    Daniel Rudy
    Mar 31, 2006
  3. Replies:
    3
    Views:
    1,071
  4. TiloVillwock

    struct array within struct

    TiloVillwock, May 15, 2010, in forum: C Programming
    Replies:
    11
    Views:
    693
    Nick Keighley
    May 16, 2010
  5. Tuan  Bui
    Replies:
    14
    Views:
    461
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page