7 17 3 DEF_ARRAY Examples

LANSA Technical

7.17.3 DEF_ARRAY Examples

These examples apply to the DEF_ARRAY command.

Example 1: The data dictionary contains 3 packed decimal fields of length 7 with 2 decimals called VAL01, VAL02 and VAL03.

Group them all into an array called #VAL that will be indexed by field #II:

DEF_ARRAY NAME(#VAL) INDEXES(#II) OF_FIELDS(#VAL01 #VAL02 #VAL03)
 

Example 2: Using array #VAL, request that the user inputs all 3 values, then check that they are all in the range 7 to 42:

DEF_ARRAY NAME(#VAL) INDEXES(#II) OF_FIELDS(#VAL01 #VAL02 #VAL03)
 
REQUEST   FIELDS(#VAL01 #VAL02 #VAL03)
 
BEGINCHECK
    BEGIN_LOOP FROM(1) TO(3) USING(#II)
    RANGECHECK FIELD(#VAL#II) RANGE((7 42))
    END_LOOP
ENDCHECK
 

The example demonstrates how the data validation commands VALUECHECK, RANGECHECK, FILECHECK, DATECHECK, CONDCHECK, CALLCHECK and SET_ERROR not only set an error for the array entry (#VAL#II) but also for the underlying field #VAL01, #VAL02 or #VAL03 (depending on the current value of #II).

 

Example 3: Using array #VAL, request the user inputs all 3 values, increment all values by 10 % and show the total to the user:

DEF_ARRAY NAME(#VAL) INDEXES(#II) OF_FIELDS(#VAL01 #VAL02 #VAL03)
REQUEST   FIELDS(#VAL01 #VAL02 #VAL03)
CHANGE  FIELD(#RESULT) TO(0)
BEGIN_LOOP FROM(1) TO(3) USING(#II)
CHANGE  FIELD(#VAL#II) TO('#VAL#II * 1.10')
CHANGE  FIELD(#RESULT) TO('#RESULT + #VAL#II')
END_LOOP
DISPLAY FIELDS(#RESULT)
 

Example 4: The data dictionary contains an alphanumeric field of length 50 called #LIBLST. It is actually an "array" of up to 5 library names stored in one long field.

Define an array so that individual library names within it can be easily referenced:

DEF_ARRAY NAME(#LIB) TYPE(*CHAR) TOT_ENTRY(5) ENTRY_LEN(10) INDEXES(#JJ) OVERLAYING(#LIBLST 1)
 

Example 5: Write a program so that field #LIBLST can be fetched from file USERDET, displayed on the screen as 5 separate fields, validated, and then updated back into the database:

DEF_ARRAY NAME(#LIB) TYPE(*CHAR) TOT_ENTRY(5) ENTRY_LEN(10) INDEXES(#JJ) OVERLAYING(#LIBLST 1)
 
DEFINE FIELD(#LIB01) TYPE(*CHAR) LENGTH(10) TO_OVERLAY(#LIBLST 1)
DEFINE FIELD(#LIB02) REFFLD(#LIB01) TO_OVERLAY(#LIBLST 11)
DEFINE FIELD(#LIB03) REFFLD(#LIB01) TO_OVERLAY(#LIBLST 21)
DEFINE FIELD(#LIB04) REFFLD(#LIB01) TO_OVERLAY(#LIBLST 31)
DEFINE FIELD(#LIB05) REFFLD(#LIB01) TO_OVERLAY(#LIBLST 41)
DEFINE FIELD(#LIBWRK) REFFLD(#LIB01)
 
FETCH FIELDS(#LIBLST) FROM_FILE(USERDET) WITH_KEY(*USER)
REQUEST FIELDS(#LIB01 #LIB02 #LIB03 #LIB04 #LIB05)
BEGINCHECK
BEGIN_LOOP FROM(1) TO(5) USING(#JJ)
  IF     COND('#LIB#JJ *NE *BLANKS')
  CHANGE FIELD(#LIBWRK) TO(#LIB#JJ)
  EXEC_OS400 COMMAND('CHKOBJ QSYS/#LIBWRK *LIB') IF_ERROR(L10)
  GOTO L20
  L10:  SET_ERROR FOR_FIELD(#LIB#JJ)
  L20:  ENDIF
END_LOOP
ENDCHECK
UPDATE FIELDS(#LIBLST) IN_FILE(USERDET)
 

You can see from this example that there would have been less code in the program if field #LIBLST in file USERDET had actually been defined in the file as five fields called #LIB01 -> #LIB05.

You will also note that EXEC_OS400 is one of the very few commands that will not accept indexed field references, which is why the work field #LIBWRK is required.

Example 6: Write a program so that field #LIBLST can be fetched from file USERDET and then printed on a report as one column:

DEF_ARRAY NAME(#LIB) TYPE(*CHAR) TOT_ENTRY(5) ENTRY_LEN(10) INDEXES(#JJ) OVERLAYING(#LIBLST 1)
 
OVERRIDE  FIELD(#LIB#JJ) COLHDG('Library' 'Names')
 
DEF_LINE  NAME(#LINE01) FIELDS(#LIB#JJ)
 
FETCH FIELDS(#LIBLST) FROM_FILE(USERDET) WITH_KEY(*USER)
 
BEGIN_LOOP FROM(1) TO(5) USING(#JJ)
      IF  COND('#LIB#JJ *NE *BLANKS')
      PRINT  LINE(#LINE01)
      ENDIF
END_LOOP
ENDPRINT
 

Example 7: A SALES file contains one field called SALDATA that consists of MONTHS (as a 12 * character (2) array), and then EXPECTED SALES and ACTUAL SALES (as two separate 12 * packed decimal (7,2) decimal arrays).

Define arrays that will allow indexed references to any component of the 3 arrays imbedded in this large field.

DEF_ARRAY NAME(#MTH) TYPE(*CHAR) TOT_ENTRY(12) ENTRY_LEN(2) INDEXES(#II) OVERLAYING(#SALDATA 1)
DEF_ARRAY NAME(#EXP) TYPE(*DEC) TOT_ENTRY(12) ENTRY_LEN(7) ENTRY_DEC(2) INDEXES(#II) OVERLAYING(#SALDATA 25)
DEF_ARRAY NAME(#ACT) TYPE(*DEC) TOT_ENTRY(12) ENTRY_LEN(7) ENTRY_DEC(2) INDEXES(#II) OVERLAYING(#SALDATA 73)
 

Example 8: Using the file from example 7, print all records from the file in columns and produce grand totals:

DEF_ARRAY NAME(#MTH) TYPE(*CHAR) TOT_ENTRY(12) ENTRY_LEN(2) INDEXES(#II) OVERLAYING(#SALDATA 1)
OVERRIDE  FIELD(#MTH#II) COLHDG('Month')
DEF_ARRAY NAME(#EXP) TYPE(*DEC) TOT_ENTRY(12) ENTRY_LEN(7) ENTRY_DEC(2) INDEXES(#II) OVERLAYING(#SALDATA 25)
OVERRIDE  FIELD(#EXP#II) COLHDG('Expected' 'Sales') EDIT_CODE(3)
DEF_ARRAY NAME(#ACT) TYPE(*DEC) TOT_ENTRY(12) ENTRY_LEN(7) ENTRY_DEC(2) INDEXES(#II) OVERLAYING(#SALDATA 73)
OVERRIDE  FIELD(#ACT#II) COLHDG('Actual' 'Sales') EDIT_CODE(3)
DEF_LINE  NAME(#SALES) FIELDS(#MTH#II #EXP#II #ACT#II)
DEFINE    FIELD(#EXP_TOT) REFFLD(#EXP#II) LABEL('Total Expected')
DEFINE    FIELD(#ACT_TOT) REFFLD(#ACT#II) LABEL('Total Actual')
DEF_BREAK NAME(#TOTAL) FIELDS(#EXP_TOT #ACT_TOT)
SELECT  FIELDS(#SALDATA) FROM_FILE(SALES)
    BEGIN_LOOP FROM(1) TO(12) USING(#II)
    KEEP_TOTAL OF_FIELD(#EXP#II) IN_FIELD(#EXP_TOT)
    KEEP_TOTAL OF_FIELD(#ACT#II) IN_FIELD(#ACT_TOT)
    PRINT LINE(#SALES)
    END_LOOP
ENDSELECT
ENDPRINT  
* automatically prints grand total lines