ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Data Structure Array Lookup

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

  • Data Structure Array Lookup

    In my program, I have defined a data structure array as follows:
    Code:
    [FONT=courier new]D Max             C                   1000                      
    D Arydta          DS                  Dim(Max) Qualified        
    D  arInv#                        7p 0 Overlay(AryDta:*next) inz 
    D  arDate                        6p 0 Overlay(AryDta:*next) inz 
    D  arInvAmt                      9p 2 Overlay(AryDta:*next) inz 
    D  ArMemAmt                      9p 2 Overlay(AryDta:*next) inz 
    D  ArSfBal                       9  2 Overlay(AryDta:*next) inz 
    D  ArIapam                       9  2 Overlay(AryDta:*next) inz 
    D  ArIajam                       9  2 Overlay(AryDta:*next) inz 
    D  ArHapam                            Overlay(AryDta:*next) inz 
    D  ArHajam                            Overlay(AryDta:*next) inz 
    D  ArSfDesc                           Overlay(AryDta:*next) inz [/FONT]
    How can I perform a lookup to see in a specific Inv# exists in Arydta.arInv# before taking action. I have not been able to get any variation of %lookup to compile using an array data structure, so I ended up doing it this way by checking each element.
    Code:
    For Ix = 1 to Max ;                    
       If Arinvn = AryDta(ix).Arinv#;      
      Found = 'Y';                         
      Leave;                               
     Endif;                                
    Endfor;
    Is there a way to do this using %lookup without re-defining the DS

  • #2
    This compiled for me didn't test though. What version are you at, I know new features were added for array data structures in 7.1.

    Code:
    D Arydta          DS                  Dim(1000) Qualified
         D  arInv#                        7p 0 Overlay(AryDta:*next) inz
         D  arDate                        6p 0 Overlay(AryDta:*next) inz
         D  arInvAmt                      9p 2 Overlay(AryDta:*next) inz
         D  ArMemAmt                      9p 2 Overlay(AryDta:*next) inz
         D  ArSfBal                       9  2 Overlay(AryDta:*next) inz
         D  ArIapam                       9  2 Overlay(AryDta:*next) inz
         D  ArIajam                       9  2 Overlay(AryDta:*next) inz
    
           Dcl-S found packed(5);
           dcl-s number packed(1) inz(1);
    
    
           found = %lookup(number:arydta(*).arinv#);

    Comment


    • #3
      Originally posted by jj_dahlheimer View Post
      This compiled for me didn't test though. What version are you at, I know new features were added for array data structures in 7.1.

      Code:
      D Arydta DS Dim(1000) Qualified
      D arInv# 7p 0 Overlay(AryDta:*next) inz
      D arDate 6p 0 Overlay(AryDta:*next) inz
      D arInvAmt 9p 2 Overlay(AryDta:*next) inz
      D ArMemAmt 9p 2 Overlay(AryDta:*next) inz
      D ArSfBal 9 2 Overlay(AryDta:*next) inz
      D ArIapam 9 2 Overlay(AryDta:*next) inz
      D ArIajam 9 2 Overlay(AryDta:*next) inz
      
      Dcl-S found packed(5);
      dcl-s number packed(1) inz(1);
      
      
      found = %lookup(number:arydta(*).arinv#);
      7.3

      Comment


      • #4
        The %Lookup as described by jj should work fine. But I'm fascinated by the use of Overlay in your DS. I've seen lot of people do this lately and it makes no sense to me - in fact it is confusing. Is this some kind of weird shop standard or did you follow an example? To me this is what the DS should look like (second version preferred).
        Code:
             D Arydta          DS                  Dim(1000) Qualified Inz
             D  arInv#                        7p 0
             D  arDate                        6p 0
             D  arInvAmt                      9p 2
             D  ArMemAmt                      9p 2
             D  ArSfBal                       9  2
             D  ArIapam                       9  2
             D  ArIajam                       9  2
        
               Dcl-ds ArydtaFree Dim(1000) Qualified Inz;
                 arInv#    packed(7);
                 arDate    packed(6);
                 arInvAmt  packed(9:2);
                 ArMemAmt  packed(9:2);
                 ArSfBal   zoned(9:2);
                 ArIapam   zoned(9:2);
                 ArIajam   zoned(9:2);
               End-ds;

        Comment


        • #5
          Originally posted by JonBoy View Post
          Is this some kind of weird shop standard or did you follow an example?
          I copied it from another program

          Comment


          • #6
            That explains it. You should probably beat the original author over the head. It is a bad day when RPG involves more typing than COBOL <grin>.

            The only time that overlay technique is needed is in cases such as group fields, or when simply redefining (say) a phone number as its component parts. It is never needed when just defining a straight DS as all fields are contiguous by definition.

            Comment


            • #7
              First off, many thanks to you all for helping me understand how to use multi-dimensional and embedded data structures. I got my first multi-dimensional array implementation to work today, sort of. Now for the weird part. Here is the embedded data structure/multi dimensional array I coded:

              Code:
                 dcl-ds locations dim(100) Qualified;
                         locn packed(2);
                         name char(35);
                         cnt zoned(8)inz;
                         lcnt zoned(8)inz;
                         dcl-ds metals dim(25);
                           mtype char(2);
                           mdesc char(20);
                           mqty zoned(6) inz;
                         end-Ds;
                       end-Ds;
              I populate it in the *INZSR as follows:
              Code:
                Begsr *Inzsr;
                         Dou %Eof(Locmst);
                           Read Locmst;
                           If not %Eof(Locmst);
                             If Locono < 55 ;
                               Mx += 1;
              
                               wkName = Loname;
                               Ix = %Scan('-':wkName);
                               Ix -= 1;
                               Monitor;
                                 wkName = %Subst(wkName:1:Ix);
                               On-error;
                               Endmon;
              
                               locations(Mx).locn = Locono;
                               locations(Mx).name = wkName;
                               For Nx =  1 to %Elem(aryMtyp);
                                 locations(Mx).metals(Nx).mtype = aryMtyp(Nx);
                                 locations(Mx).metals(Nx).mdesc = aryMdesc(Nx);
                                 locations(Mx).metals(Nx).mqty = 0;
                               Endfor;
              
                             Endif;
                           Endif;
                         Enddo;
              
                       Endsr;
              I started noticing that at the very initialization stage of my program, LOCATIONS(nn).locn contains the value 04 for all 100 elements of LOCATIONS. this causes my program to abort farther into the program.

              Click image for larger version

Name:	Capture.JPG
Views:	4054
Size:	112.3 KB
ID:	150763

              Whats weird is I can insert a CLEAR LOCATIONS as the first statement in the *INZSR and it clears LOCATIONS(nn).locn for all 100 elements and my program doesn't abort. Any explanations? I shouldn't have to clear the LOCATIONS first thing, right? Maybe I should just forget about it since I found a workaround.

              Comment


              • #8
                Well if the Clear fixes it then having INZ on the Locations DS itself should do the same thing. You don't need the individual INZs. You don't say exactly what the error message was or what field(s) we're involved so it is tough to be sure. But certainly the INZ would do the job without the need for the Clear.

                You don't show where Locono is coming from - is it in the Locmast record? You have no logic that could change it other than the Read that I can see.

                By the way - there's no need to use Monitor and it is so easy to avoid in this case. I think this code does the same thing a little more cleanly but please check.
                Code:
                Ix = %Scan('-':wkName);  
                If Ix > 0;
                   wkName = %Subst(wkName:1: ( Ix - 1));  // Just grab the front bit  
                EndIf:
                Reason for doing this is that the errors captured by monitor will clog up the log.
                Last edited by JonBoy; July 22, 2019, 02:18 PM. Reason: [code] tag not working properly ...

                Comment


                • #9
                  False alarm........sorry. I forgot to initialize LOCATIONS.locn

                  Comment


                  • #10
                    That's why you do the Int on the DS <grin>

                    Comment


                    • Vectorspace
                      Vectorspace commented
                      Editing a comment
                      Do you mean Inz..?

                  • #11
                    Originally posted by JonBoy View Post
                    You don't show where Locono is coming from - is it in the Locmast record?
                    Yes, it is in LOCMAST

                    By the way - there's no need to use Monitor and it is so easy to avoid in this case. I think this code does the same thing a little more cleanly but please check.
                    Code:
                    Ix = %Scan('-':wkName);
                    If Ix > 0;
                    wkName = %Subst(wkName:1: ( Ix - 1)); // Just grab the front bit
                    EndIf:
                    Reason for doing this is that the errors captured by monitor will clog up the log.
                    Good thought, I'll change it

                    Comment


                    • #12
                      The multi dimensional array/embedded structure concept described here is going to revolutionize my programming for this project. I am in the process of modifying a program that has 18,000+ lines of code. Most of it is subroutines that are identical except for different fields names. We have 15 locations in our company. this program has a subroutine routine for each locn that calculates how many items were transferred to each of the other 14 locns. The 15 subr's are alike except different field names. This is a perfect candidate for multi dimensional arrays. I'm curious how many of the 18,000+ lines of code I can eliminate.

                      Comment


                      • #13
                        You should look seriously at changing the subroutines to subprocedures too.

                        Comment


                        • #14
                          I got a 3 dimensional array to work today in a program that had 18,000 lines of code. I got it down to 750 lines of code.

                          Comment

                          Working...
                          X