ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Data Structure Array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data Structure Array

    Ok, here's the general gist:
    1) Trying to print out how many sales calls a sales consultant talks to.
    2) Using a data structure array to do this:
    * Consultant = consultant name.
    * Count = increment by one each time another record for this consultant comes through.
    3) Having some problems with %LOOKUP & %XFOOT.
    4) Code with comments documenting the logic is listed below.

    Code:
     *   Data structure to count number of calls
    D wdConsCount     DS                  Dim(20)  Qualified
    D   wdCons                       8A   Overlay(wdConsCount)
    D   wdCount                      3S 0 Overlay(wdConsCount : *Next)
     *********************************************************************
     /FREE
      //******************************************************************
      BegSR ProcessRecord; 
      //------------------------------------------------------------------  
                                                                                
         // wdConsCount is a Data Structure array that tracks the Consultant
         // and the number of calls they have taken.
    
         // wwMaxIdx = last index number assigned in the wdConsCount data
         // structure array.
                                                                                
         // If zero, first time assigning to a consultant.
         If (wwMaxIdx = *Zeros);                
    
    
           wdConsCount(1).wdCons   = dAsnCn;   // Use element #1 as index  
           wdConsCount(1).wdCount  = 1;        // Set initial count to 1 
           wwMaxIdx = 1;                       // Init "max index" to 1
    
        // If not zero, at least one consultant assigned to wdConsCount
        // data structure array
        Else;
    
           // Use %Lookup to determine if this consultant is already
           // in the array
           ww = %LOOKUP(dAsnCn : wdConsCount.wdCons);
           If (ww = *Zeros);     // Zero = not yet in data struct array
              wwMaxIdx += 1;     // Increment "maximum index" by one
              wdConsCount(wwMaxIdx).wdCons  = dAsnCn;  // Set consult
              wdConsCount(wwMaxIdx).wdCount = 1;    // Init count to one
           // If consultant already exists in data structure array...
           Else;
              wdConsCount(ww).wdCount += 1;    // Increment counter by one 
           EndIf;                  
    
         EndIf;
    
      EndSR;
      //******************************************************************
      BegSR LRTotals;
      //------------------------------------------------------------------
    
         // Sort consultants ascedning alpha order
         SortA wdConsCount.wdCons;
    
         // Spin through all consultants to print
         For ww = 1 to wwMaxIdx;
                                                                              
            // O-Spec fields names must be 14 chars long or shorter.
            // Move fields from data structure to standalone fields with
            // shorter names
            wwCons  = wdConsCount(ww).wdCons;
            wwCount = wdConsCount(ww).wdCount;
              wwCount = wdConsCount(ww).wdCount;
             Except LRTotPrint;   // Print contultant total information
    
          EndFor;
    
          // Get grand number of calls for all consultants & print
          wwTotalCalls = %XFOOT(wdConsCount.wdCount);
          Except TotalCalls;
    
       EndSR;
       //******************************************************************
      /END-FREE
    *********************************************************************
    Last edited by Chipper; May 8, 2009, 10:47 AM. Reason: Fixed SortA -- needs to show that I'm sorting by consultant name
    http://www.linkedin.com/in/chippermiller

  • #2
    Re: Data Structure Array

    I just took a quick look, so I may be off, but I dont think you need the overlay keyword on each subfield.
    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

    Comment


    • #3
      Re: Data Structure Array

      Originally posted by MichaelCatalani View Post
      I just took a quick look, so I may be off, but I dont think you need the overlay keyword on each subfield.
      Thanks for the heads up. Removed them but still getting the same error messages as before. These messages apply to the code above but I forgot to post them earlier:

      RNF0555 The parameter for %XFOOT is not valid; %XFOOT is ignored.
      RNF0571 The second parameter for %LOOKUPxx is not valid.
      RNF5159 Factor 2 operand must be an array name for specified operation.
      RNF7591 An operand of a qualified name expression is not valid.
      http://www.linkedin.com/in/chippermiller

      Comment


      • #4
        Re: Data Structure Array

        If I'm not mistaken, in your XFoot statement,

        wwTotalCalls = %XFOOT(wdConsCount.wdCount);

        The wdConsCount.wdCount is an element, not an array. The XFoot does a summation of the Array. Does it not? So, you'd have do have a separate Array for wdCount to utilize the %XFoot bif.

        Arrays aren't my power suit .. but, I have dabbled before...

        And, the same would go for your %Lookup -- you're doing a lookup on an element, not an array.
        Last edited by FaStOnE; May 8, 2009, 11:18 AM.

        Comment


        • #5
          Re: Data Structure Array

          I'm not sure they have implemented xfooting across a data structure array. (If they did, I would like to know about it.)

          As far as I'm aware, you have to cross-add by using a for loop.

          ie:
          Code:
           
          wwTotalCalls = 0;
           
          for  x = 1 to %elem( wdConsCount );
           
            wwTotalCalls += wdConsCount.wdCount;
           
          endfor;

          You should be able to do the %lookup on the array name of wdConsCount. I dont think you can specify a qualfied subfield, although it would be nice that you could. In this case it wouldnt matter though, as it should find the Consultant name in the array even with the counts and consultant names in the array.

          So just change the line:

          ww = %LOOKUP(dAsnCn : wdConsCount.wdCons);

          to

          ww = %LOOKUP(dAsnCn : wdConsCount);
          Last edited by MichaelCatalani; May 8, 2009, 11:27 AM.
          Michael Catalani
          IS Director, eCommerce & Web Development
          Acceptance Insurance Corporation
          www.AcceptanceInsurance.com
          www.ProvatoSys.com

          Comment


          • #6
            Re: Data Structure Array

            Originally posted by MichaelCatalani View Post
            I'm not sure they have implemented xfooting across a data structure array. (If they did, I would like to know about it.)

            As far as I'm aware, you have to cross-add by using a for loop.

            ie:
            Code:
             
            wwTotalCalls = 0;
             
            for  x = 1 to %elem( wdConsCount );
             
              wwTotalCalls += wdConsCount.wdCount;
             
            endfor;
            I was hoping there was a way to do it without having to resort to this method, but I'm not going to shoot the messenger.
            Last edited by Chipper; May 8, 2009, 12:47 PM. Reason: Fix formatting -- forgot end [/quote] when removing part of original quoted msg
            http://www.linkedin.com/in/chippermiller

            Comment


            • #7
              Re: Data Structure Array

              like this?

              PHP Code:
              d group           ds                  qualified  inz                         
              d  WholeTomato                2900                                           
              d   DataString                  29    dim
              (100overlay(WholeTomato:*next)    
              d    Container                   2    overlay(dataString:1)                  
              d    Group                       2  0 overlay(datastring:*next)              
              d    Count                       3  0 overlay(dataString:*next)              
              d    NewName                    12    overlay(dataString:*next)              
              d   Location                    10    overlay(datastring:*next)              
              d    CG                          4    overlay(dataString:1
              All my answers were extracted from the "Big Dummy's Guide to the As400"
              and I take no responsibility for any of them.

              www.code400.com

              Comment


              • #8
                Re: Data Structure Array

                Methinks I muddied the water by trying to throw all the code in at once. Let me take each situation one by one and attack each compiler error.

                This part is based on the entry called "Sorting an Array - Example"

                Is that just a regular array as opposed to a data structure array?

                Code:
                 *   Data structure to count number of calls                       
                D wdConsCount     DS                  Qualified  Dim(2)            
                D   wdCons                       8A   Overlay(wdConsCount)         
                D   wdCount                      3S 0 Overlay(wdConsCount : *Next) 
                 ******************************************************************
                 /FREE                                                             
                  //***************************************************************
                                                                                   
                     // Sort so wdConsCount is in ascending order by consultant    
                     SortA wdCons;                                                 
                                                                                   
                  //***************************************************************
                 /END-FREE
                ******************************************************************
                http://www.linkedin.com/in/chippermiller

                Comment


                • #9
                  Re: Data Structure Array

                  Hey Chipper:

                  Note qualified and dim are on different lines than what you have defined. This compiles ok....results below. Also note after sorta the wdcount also got sorted.

                  Code:
                  D wdconscount     DS                  qualified                     
                  D   ConsCount                   11a   Dim(2)                        
                  D   wdCons                       8A   Overlay(ConsCount)            
                  D   wdCount                      3S 0 Overlay(ConsCount : *Next)    
                  d   total         s              5i 0                               
                   /FREE                                                              
                    //************************************************** 
                        wdconscount.wdcons(1) = 'wxyz';                        
                        wdconscount.wdcons(2) = 'abcd';                        
                        wdconscount.wdcount(1) = 100;                          
                        wdconscount.wdcount(2) = 200;                          
                        // Sort so wdConsCount is in ascending order by consult
                        SortA wdconscount.wdCons;                              
                        total = %xfoot(wdconscount.wdcount);                                                                                       
                  
                    *inlr = *on;
                  WDCONSCOUNT as seen in debug after sorta and total.
                  WDCONSCOUNT.CONSCOUNT(1) = 'abcd 200'
                  WDCONSCOUNT.CONSCOUNT(2) = 'wxyz 100'
                  WDCONSCOUNT.WDCONS(1) = 'abcd '
                  WDCONSCOUNT.WDCONS(2) = 'wxyz '
                  WDCONSCOUNT.WDCOUNT(1) = 200.
                  WDCONSCOUNT.WDCOUNT(2) = 100.
                  TOTAL = 300
                  Best of Luck
                  GLS
                  The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

                  Comment


                  • #10
                    Re: Data Structure Array

                    Originally posted by jamief View Post
                    like this?

                    PHP Code:
                    d group           ds                  qualified  inz                         
                    d  WholeTomato                2900                                           
                    d   DataString                  29    dim
                    (100overlay(WholeTomato:*next)    
                    d    Container                   2    overlay(dataString:1)                  
                    d    Group                       2  0 overlay(datastring:*next)              
                    d    Count                       3  0 overlay(dataString:*next)              
                    d    NewName                    12    overlay(dataString:*next)              
                    d   Location                    10    overlay(datastring:*next)              
                    d    CG                          4    overlay(dataString:1
                    I have the same problem, i defined de data structure array as jamief says, and it works, i can do a lookup successfully, but the problem is how to assign values to the fields, in my case fields are prod/regis/monto... i tried many ways, but still can't compile.

                    PHP Code:
                    D allRecords      ds                  qualified inz                    
                    D  wholeAll                   2400                                     
                    D   allData                     24    dim
                    (100overlay(wholeAll:*next)
                    D    prod                        6    overlay(allData:1)               
                    D    regis                       6  0 overlay(allData:*next)           
                    D    monto                      12  2 overlay(allData:*next)           


                    // lookup works fine:
                    pos = %lookup(xxx:allRecords.prod);

                    // but how to assign values to prod??? 

                    Comment


                    • #11
                      Re: Data Structure Array

                      Code:
                      allRecords.prod = 'whatever';
                      I'm not anti-social, I just don't like people -Tommy Holden

                      Comment


                      • #12
                        Re: Data Structure Array

                        Code:
                        d count           s              3  0                                      
                        d pos             s              3  0                                      
                         *                                                                         
                        d allRecords      ds                  qualified inz                        
                        d   allData                     24    dim(100) overlay(allrecords:*next)   
                        d    prod                        6    overlay(allData:1)                   
                        d    regis                       6  0 overlay(allData:*next)               
                        d    monto                      12  2 overlay(allData:*next)               
                         *                                                                         
                         /free                                                                     
                              for count = 1 to 3;                                                  
                               allrecords.alldata(count)  = 'DTA'  + %editc(count:'X') +           
                                                            %editc(%dec((12 * count):6:0):'X') +   
                                                            '9999999999.99';                       
                              endfor;                                                              
                              pos = %lookup('DTA002':allRecords.prod);                             
                              allrecords.prod(pos) = 'jamieX';                                     
                              pos = %lookup('jamieX':allRecords.prod);                             
                                                                                                   
                              *inlr = *on;                                
                         /end-free
                        All my answers were extracted from the "Big Dummy's Guide to the As400"
                        and I take no responsibility for any of them.

                        www.code400.com

                        Comment


                        • #13
                          Re: Data Structure Array

                          thanks jamie!

                          allrecords.prod(pos) = 'jamieX';

                          was the assignation way i think i didn't tried...

                          problem solved!
                          thanks!

                          Comment


                          • #14
                            Re: Data Structure Array

                            Originally posted by jamief View Post
                            Code:
                            d count           s              3  0                                      
                            d pos             s              3  0                                      
                             *                                                                         
                            d allRecords      ds                  qualified inz                        
                            d   allData                     24    dim(100) overlay(allrecords:*next)   
                            d    prod                        6    overlay(allData:1)                   
                            d    regis                       6  0 overlay(allData:*next)               
                            d    monto                      12  2 overlay(allData:*next)               
                             *                                                                         
                             /free                                                                     
                                  for count = 1 to 3;                                                  
                                   allrecords.alldata(count)  = 'DTA'  + %editc(count:'X') +           
                                                                %editc(%dec((12 * count):6:0):'X') +   
                                                                '9999999999.99';                       
                                  endfor;                                                              
                                  pos = %lookup('DTA002':allRecords.prod);                             
                                  allrecords.prod(pos) = 'jamieX';                                     
                                  pos = %lookup('jamieX':allRecords.prod);                             
                                                                                                       
                                  *inlr = *on;                                
                             /end-free
                            Not that one can improve on perfection but ...

                            The length on the group field allData is not required (just as it is not on a DS) the compiler calculates the length from the sum of the fields that overlay it. I think it makes it more obvious that the field is just a group entity when there is no length defined. When you include the length to me it suggests that it is "important" when in this case (other than to the compiler) it is irrelevant.


                            Jon P.

                            Comment


                            • #15
                              Re: Data Structure Array

                              Here is also a better example in MC Press Article..


                              http://www.mcpressonline.com/program...ul-arrays.html


                              Pramendra Pandeya
                              Young people knows how to run fast but old people knows the way..

                              Comment

                              Working...
                              X