SORTA with %SUBARR

Built-in function %SUBARR returns a section of the specified array starting at start-index. The number of elements returned is specified by the optional number-of-elements parameter. If not specified, the number-of-elements defaults to the remainder of the array. The first parameter of %SUBARR must be an array. That is, a standalone field, data structure, or subfield defined as an array. The first parameter must not be a table name or procedure call. The code below illustrates a three hundred element array of 138-byte entries. The first 31 bytes are going to be used to order the sequence of the array entries, after the data has been loaded.


     D                 DS                  INZ
     D trlArry                      138a   DIM(300)
     D   traSRT                      31a   Overlay(trlArry)

     D lodArry                        4s 0 DIM(300)

     D                 DS                  INZ
     D trlData                      138a
     D   trxEBD                       8s 0 Overlay(trlData)
     D   trxEBT                       6s 0 Overlay(trlData:*next)
     D   trxGRP                       5s 0 Overlay(trlData:*next)
     D   trxLDT                       8s 0 Overlay(trlData:*next)
     D   trxLDN                       4s 0 Overlay(trlData:*next)
     D   trxTRL                       4a   Overlay(trlData:*next)
     D   trxKDL                      18a   Overlay(trlData:*next)
     D   trxPRD                      12a   Overlay(trlData:*next)
     D   trxLBD                       8s 0 Overlay(trlData:*next)
     D   trxLBT                       6s 0 Overlay(trlData:*next)
     D   trxLPD                       8s 0 Overlay(trlData:*next)
     D   trxLPT                       6s 0 Overlay(trlData:*next)
     D   trxVCD                       8s 0 Overlay(trlData:*next)
     D   trxVCT                       6s 0 Overlay(trlData:*next)
     D   trxEPD                       8s 0 Overlay(trlData:*next)
     D   trxEPT                       6s 0 Overlay(trlData:*next)
     D   trxSCD                       8s 0 Overlay(trlData:*next)
     D   trxSCT                       6s 0 Overlay(trlData:*next)
     D   trxIND                       1a   Overlay(trlData:*next)
     D   trxSTS                       2a   Overlay(trlData:*next)

                      

The code below represents the process--the data structures are cleared (resetting the elements of the array). And an SQL cursor is opened to loop through the data records to load the data structure fields.


          Exec SQL Open TrlCursor;

          TrlCsrOpn = *On;
          CLEAR trlcDS   ;
          CLEAR trlData;
          CLEAR prvData;
          CLEAR CurSchedule;
          CLEAR prvSchedule;
          CLEAR previousSts;

          Dou SQLSTT = SQLstsEOF OR trls > 299;

             Exec SQL
                 Fetch TrlCursor Into :SelectedTRL, :SelectedLOD;

             If SQLSTT =  SQLstsOK;

	 // load trailer data into the data structure     	
	 // When the data fields have been populated
	 // increment the count and move the contents 
         // of the array to the data structure

               TRLS += 1;
                trlArry(trls) = trlData;
            EndIf;

         EndDO;

          // Once the array has been loaded, sort the array
          // and load the subfile using the array elements

          If trls > 0;
             SORTA %subarr(traSRT:1:trls);
          EndIf;
                                           

Once the data has been moved to the DS fields, the resulting 138-bytes are pushed to the array. Then the array is sorted, based on the first 31-bytes rather than the entire array element using %SUBARR in conjunction with the SORTA Op code. This will be used to load a subfile for presentation, in the sequence required by the user. How the data is retrived and the sequence in which it was retrieved may be completely different than how the data is displayed.