Thread: Read Table Record and Write to Array

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    26

    Read Table Record and Write to Array

    I have been tasked with what I hope I explain in a way that is understandable.

    In this data pull, the user will have an option to limit the number of Orders to pull for a given Customer. We will assume 3 orders, but they may have multiple lines per order, we need to include those lines.

    1. Read table record, search array, if customer found, check if order exists and if the order count is 3 or less write array if order limit reached and in the array for customer bypass until new customer is read.
    2. Need to keep count of orders processed are within the order limit of 3.
    3. Repeat for records in the table and search array.


    In the attached file, I have a sample of the Table data that will be read and written to the array.

    I also have the code I would tweak to hopefully make this happen.

    Thank you in advance,
    FrankCLT
    Attached Images Attached Images

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Pro tip! Post your code with the provided tags.
    Next pro tip.. State, clearly, how the data is stored. I assume you are working with ascii text data... Am I right?

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    I've posted the .pdf containing the snippet of code that i am tweaking for the process outlined in the post. I can post the actual code from the .c file if thats what you mean? As for the data, not really sure on how its stored. I use SQL for access and i just pasted the output from a SQL query to excel.

    Thank you,
    FrankCLT

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Regardless, it's still a fuzzy picture, not actual text we can copy/paste.

    I'd put your text into a program to show you.

    No way am I going to waste 15 minutes typing in your data, when you could copy/paste it in 5 seconds.

    If you want people to answer, make it EASY for them to answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Apologies, that is last thing I wanted to do is make it more difficult and time consuming for those helping.

    Here is a copy and paste of my code that I use for another task but would be my template for my new task.

    Code:
    			   JDEDBReturn1 = JDB_SelectKeyed(hReqF42119, (ID)0, (void *) NULL, (short)(0));
    			   while (JDEDBReturn1 == JDEDB_PASSED)
    			   {
    				   JDEDBReturn1 = JDB_Fetch(hReqF42119, (void *) &dsF42119, (int)0);
    				   if (JDEDBReturn1 == JDEDB_PASSED)
    				   {
    					   bFound = FALSE;  
    					   vRecRead = 0;
    					   while (vRecRead < vMaxRecs && !bFound)
    					   {
    						   if (MathCompare(&aHistory[vRecRead].mnAddressNumberShipTo, &dsF42119.sdshan)==0)
    						   {
    							   bFound = TRUE;
    						   }
    						   else
    						   {
    							   vRecRead++;
    						   }
    					   } 
    					   if (bFound)
    					   {
    						   /* exists in array - accumulate totals */
    						   MathAdd(&aHistory[vRecRead].mnUnitsQuantityShipped, &aHistory[vRecRead].mnUnitsQuantityShipped, &dsF42119.sdsoqs);
    						   MathAdd(&aHistory[vRecRead].mnAmountOrderGross, &aHistory[vRecRead].mnAmountOrderGross, &dsF42119.sdaexp);
    					   }
    					   else
    					   {
    						   /* does NOT exist in arrey - copy to array */
    						   MathCopy(&aHistory[vRecRead].mnAddressNumberShipTo, &dsF42119.sdshan); 
    						   MathCopy(&aHistory[vRecRead].mnDocumentOrderInvoiceE, &dsF42119.sddoco); 
    						   MathCopy(&aHistory[vRecRead].mnLineNumber, &dsF42119.sdlnid);
    						   vMaxRecs++;
    					   }
    				   }
    			   }


    Again, thank you very much,
    FrankCLT

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Code:
                        
    Table Data            What I need array to look like when filled        
    Customer     Order#        Customer     Order#    Index
    102140        3349553        102140        3349553    0
    102140        3309756        102140        3309756    1
    102140        3309756                
    102160        3330395        102160        3330395    2
    102160        3329182        102160        3329182    3
    102160        3323283        102160        3323283    4
    102160        3317115                
    102160        3305923                
    102510        3334064        102510        3334064    5
    102510        3334064                
    102750        3342812        102750        3342812    6
    102750        3330579        102750        3330579    7
    102750        3330570        102750        3330570    8
    102750        3320982                
    103137        3307385        103137        3307385    9
    103646        3321163        …    …    
    104089        3327377        …    …    
    104735        3330852        …    …

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    what is calling it?
    what is being passed to it and what does it look like?
    psudo code could be somthing like
    Code:
    get customer number and order number
    add customer number 1 to array[0][0]
    while customer number 1 add ordernumber to array [0][0]
    if ordernumber 2 = order number 1 next 
    else add order number 2 to array[0][1]
    loop
    ... next customer number

  8. #8
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Hello,

    Oracle JD Edwards E1 is a globally used ERP system and part of its development toolset is C language that they have added some Oracle specific features, but under the hood it is still C.

    To answer your questions:

    What is calling it === > It can be called from anywhere, an Interactive application, a UBE (what a report is called in JD Edwards) and many other ways as well.

    The table data in my above example is what would have been read and processed 1 record at a time.

    Doe this kind of answer your questions?

    Thank you,
    FrankCLT

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Just to be clear, the two columns you get out of the database are
    Customer Order#
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i mean what does the string look like....how is it separated out.... for example i had a similar thing the other week i had to read in a file that was formatted like this "A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","AB SOLUTELY", etc etc

  11. #11
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Salem...Yes, the columns are Customer and Order Number

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct {
        int customer;
        int order;
    } table[] = {
        { 102140, 3349553 },
        { 102140, 3309756 },
        { 102140, 3309756 },
        { 102160, 3330395 },
        { 102160, 3329182 },
        { 102160, 3323283 },
        { 102160, 3317115 },
        { 102160, 3305923 },
        { 102510, 3334064 },
        { 102510, 3334064 },
        { 102750, 3342812 },
        { 102750, 3330579 },
        { 102750, 3330570 },
        { 102750, 3320982 },
        { 103137, 3307385 },
    };
    
    int main()
    {
        const int maxOrdersPerCustomer = 3;
        int currentCustomer = -1, currentOrder = -1, orderCount = 0;
        for ( int i = 0 ; i < 15 ; i++ ) {
            if ( table[i].customer != currentCustomer ) {
                currentCustomer = table[i].customer;
                currentOrder = table[i].order;
                orderCount = 1;
                printf("C=%d, O=%d\n", currentCustomer, currentOrder);
            } else {
                if ( table[i].order != currentOrder ) {
                    currentOrder = table[i].order;
                    orderCount++;
                    if ( orderCount <= maxOrdersPerCustomer ) {
                        printf("C=%d, O=%d\n", currentCustomer, currentOrder);
                    }
                }
            }
        }
        return 0;
    }
    $ ./a.out 
    C=102140, O=3349553
    C=102140, O=3309756
    C=102160, O=3330395
    C=102160, O=3329182
    C=102160, O=3323283
    C=102510, O=3334064
    C=102750, O=3342812
    C=102750, O=3330579
    C=102750, O=3330570
    C=103137, O=3307385
    The outer for loop would just be whatever your "get next record" database call is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Wow, that was quick. Thank you for the speedy response.

    I will tweak and give you an update.

    Thank you,
    FrankCLT

  14. #14
    Registered User
    Join Date
    Sep 2018
    Posts
    26
    Hi Salem,

    I can't get that code to work. In your example the "for" assumes a 15-record count. And appears to be using the table as the array. I will read 1 record at a time search the array and write accordingly. I won't know the number of records for the "for" loop. I guess I'm just not following the logic.


    Code:
                               /* Select records */ 
    			   JDEDBReturn1 = JDB_SelectKeyed(hReqF42119, (ID)0, (void *) NULL, (short)(0));    
    			   while (JDEDBReturn1 == JDEDB_PASSED)
    			   {
                                       /* Loops Reading a single record */
    				   JDEDBReturn1 = JDB_Fetch(hReqF42119, (void *) &dsF42119, (int)0);
    				   if (JDEDBReturn1 == JDEDB_PASSED)
    				   { 
                                             / * your sample code should be here for searching Array for each record */
    				   }
    			   }
    Thank you,
    FrankCLT

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There is no requirement for a table.
    That's just an artefact of having something to show you.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    struct foo {
        int customer;
        int order;
    } table[] = {
        { 102140, 3349553 },
        { 102140, 3309756 },
        { 102140, 3309756 },
        { 102160, 3330395 },
        { 102160, 3329182 },
        { 102160, 3323283 },
        { 102160, 3317115 },
        { 102160, 3305923 },
        { 102510, 3334064 },
        { 102510, 3334064 },
        { 102750, 3342812 },
        { 102750, 3330579 },
        { 102750, 3330570 },
        { 102750, 3320982 },
        { 103137, 3307385 },
    };
     
    int main()
    {
        const int maxOrdersPerCustomer = 3;
        int currentCustomer = -1, currentOrder = -1, orderCount = 0;
    
        // There is no table, just loop until you have no more records.
        for ( int i = 0 ; i < 15 ; i++ ) {
            // don't fetch it from the table, fetch it from your database
            struct foo data = table[i];
    
            if ( data.customer != currentCustomer ) {
                currentCustomer = data.customer;
                currentOrder = data.order;
                orderCount = 1;
                printf("C=%d, O=%d\n", currentCustomer, currentOrder);
            } else {
                if ( data.order != currentOrder ) {
                    currentOrder = data.order;
                    orderCount++;
                    if ( orderCount <= maxOrdersPerCustomer ) {
                        printf("C=%d, O=%d\n", currentCustomer, currentOrder);
                    }
                }
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write datatypes to memory array
    By Synaps3 in forum C Programming
    Replies: 4
    Last Post: 10-05-2014, 04:36 PM
  2. Hash Table with Simple record in files
    By infantheartlyje in forum C Programming
    Replies: 77
    Last Post: 10-13-2011, 03:15 AM
  3. Read Each Line of File as Array Record
    By Leo2010 in forum C Programming
    Replies: 3
    Last Post: 02-23-2010, 10:13 AM
  4. How to read and write an char array
    By Menno in forum C Programming
    Replies: 0
    Last Post: 06-17-2007, 06:17 AM
  5. read/write Cstring array to file
    By bonkey in forum Windows Programming
    Replies: 1
    Last Post: 08-18-2002, 09:15 AM

Tags for this Thread