0

Ok, I realize this is a very niche issue, but I'm hoping the process is straight forward enough...

I'm tasked with creating a data file out of Customer/Order information. Problem is, the datafile has a 5 product max limit.

Basically, I get my data, group by cust_id, create the file structure, within that loop, group by product_id, rewrite the fields in previous file_struct with new product info. That's worked all well and good until a user exceeded that max.

A brief example.. (keep in mind, the structure of the array is set by another process, this CANNOT change)

orderArray = arranyew(2);
set order = 1;

loop over cust_id;

    field[order][1] = "field(1)"; // cust_id
    field[order][2] = "field(2)"; // name
    field[order][3] = "field(3)"; // phone
    field[order][4] = ""; // product_1
    field[order][5] = ""; // quantity_1
    field[order][6] = ""; // product_2
    field[order][7] = ""; // quantity_2
    field[order][8] = ""; // product_3
    field[order][9] = ""; // quantity_3
    field[order][10] = ""; // product_4
    field[order][11] = ""; // quantity_4
    field[order][12] = ""; // product_5
    field[order][13] = ""; // quantity_5
    field[order][14] = "field(4)"; // trx_id
    field[order][15] = "field(5)"; // total_cost

    counter = 0;

    loop over product_id

        field[order[4+counter] = productCode;
        field[order[5+counter] = quantity;

        counter = counter + 2;

    end inner loop;

    order = order + 1;

end outer loop;

Like I said, this worked fine until I had a user who ordered more than 5 products.

What I basically want to do is check the number of products for each user if that number is greater than 5, start a new line in the text field, but I'm stufk on how to get there.

I've tried numerous fixes, but nothing gives the results I need.

I can send the entire file if It can help, but I don't want to post it all here.

Shawn Holmes
  • 3,752
  • 22
  • 25
Gene R
  • 1,555
  • 5
  • 19
  • 32

2 Answers2

0

You need to move the inserting of the header and footer fields into product loop eg. the custid and trx_id fields.

Here's a rough idea of one why you can go about this based on the pseudo code you provided. I'm sure that there are more elegant ways that you could code this.

set order = 0;

loop over cust_id;

counter = 1;
order = order + 1;

loop over product_id
    if (counter == 1 || counter == 6) {
        if (counter == 6) {
            counter == 1;
            order= order+1;
        }
        field[order][1] = "field(1)"; // cust_id
        field[order][2] = "field(2)"; // name
        field[order][3] = "field(3)"; // phone
    }

    field[order][counter+3] = productCode; // product_1
    field[order][counter+4] = quantity; // quantity_1

    counter = counter + 1;
    if (counter == 6) {
        field[order][14] = "field(4)"; // trx_id
        field[order][15] = "field(5)"; // total_cost
    }

end inner loop;

if (counter == 6) {
    // loop here to insert blank columns and the totals field to fill out the row.
}

end outer loop;

One thing goes concern me. If you start a new line every five products then your transaction id and total cost is going to be entered into the file more than once. You know the receiving system. It may be a non-issue.

Hope this helps

Stephen Moretti
  • 2,442
  • 1
  • 18
  • 29
  • The next row has a flag to indicate its a continuation of the previous order, so a new order isn't created. – Yisroel Nov 30 '10 at 00:05
  • This sort of makes sense, but I don't think its this simple. Problem is this: My output file will look like this: ncid, fname, lname, prod1, quan1, prod2, quan2...prod5, quan5, trx_id, total_cost This layout cannot change. So if there are 20 prods, I need to create a new record where all data can be blank other than prod1-prod5 and a continuation field that I mark with X So in essence: loop over cust ncid,fname,lname loop over prod prod1,prod2...prod5 (if > 5 leave loop) trx_id,cost (now start over with new record... – Gene R Nov 30 '10 at 17:44
  • @Yiseroel Ah cool. makes sense. So Gene would need to put a continuation flag in the new row that is created when counter is 6 – Stephen Moretti Dec 01 '10 at 15:04
  • @Gene R : This pseudo code does exactly what you describe, but you don't need to leave the product loop. The only time you should leave the product loop is when there are no more products for the current customer. – Stephen Moretti Dec 01 '10 at 15:06
0

As you put the data into the row, you need check if there are more than 5 products and then create an additional line.

loop over product_id
    if (counter mod 10 == 0 and counter > 0) {
         // create the new row, and mark it as a continuation of the previous order
         counter = 0;
         order = order + 1;

         field[order][1] = "";
         ...
         field[order][15] = "";
    }

    field[order[4+counter] = productCode;
    field[order[5+counter] = quantity;

    counter = counter + 2;

end inner loop;

I've actually done the export from an ecommerce system to MOM, but that code has since been lost. I have samples of code in classic ASP.

Yisroel
  • 8,164
  • 4
  • 26
  • 26