Top
NetCOBOL V11.0 Syntax Samples
FUJITSU Software

1.3 Subscripting

When a data structure is to be used repeatedly, a table is defined using the OCCURS clause. A subscript is used to reference the individual table elements.

When using subscripting, be careful about setting and referencing elements outside the range defined with the OCCURS clause. Referencing an item using a subscript that is not within the bounds of the table can destroy an area of memory, or cause execution to terminate abnormally.

000010 @OPTIONS MAIN
000020*----------------------------------------------------------------------
000030* Data declared in an array can be referenced using a subscript.
000040*----------------------------------------------------------------------
000050 IDENTIFICATION  DIVISION.
000060 PROGRAM-ID.     SAMPLE.
000070 DATA            DIVISION.
000080 WORKING-STORAGE SECTION.
000090 01 ALP          PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
000100 01              REDEFINES ALP.
000110   02 CHAR         OCCURS 26 TIMES PIC X.
000120 01 IN-DATA      PIC X.
000130 01 COUNTER      PIC 9(2).
000140 PROCEDURE       DIVISION.
000150     DISPLAY "Please input one upper-case letter. >>" WITH NO 
000155             ADVANCING.
000160     ACCEPT IN-DATA.
000170     PERFORM TEST BEFORE
000180             VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 26
000190*----------------------------------------------------------------------
000200* COUNTER is used as a subscript for CHAR to allow comparison with the 
000210* input character.
000220*----------------------------------------------------------------------
000230       IF IN-DATA = CHAR(COUNTER) THEN
000240*----------------------------------------------------------------------
000250         EXIT PERFORM
000260       END-IF
000270     END-PERFORM.
000280     IF COUNTER <= 26 THEN
000290       DISPLAY IN-DATA " is character "
000295               COUNTER " in alphabetical order."
000300     ELSE
000310       DISPLAY "The input character is incorrect."
000320     END-IF.
000330 END PROGRAM SAMPLE.

Uniqueness of qualification and subscript reference can also be combined.

000010 @OPTIONS MAIN
000020*----------------------------------------------------------------------
000030* Data declared in an array is uniquely qualified.
000040*----------------------------------------------------------------------
000050 IDENTIFICATION   DIVISION.
000060 PROGRAM-ID.      SAMPLE.
000070 DATA             DIVISION.
000080 WORKING-STORAGE  SECTION.
000090 01 UPPER-CASE.
000100    02 ALP        PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
000110    02            REDEFINES ALP.
000120      03 CHAR       OCCURS 26 TIMES PIC X.
000130 01 LOWER-CASE.
000140    02 ALP        PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
000150    02            REDEFINES ALP.
000160      03 CHAR       OCCURS 26 TIMES PIC X.
000170 01 IN-DATA       PIC X.
000180 01 COUNTER       PIC 9(2).
000190 PROCEDURE        DIVISION.
000200     DISPLAY "Please input one upper-case letter. >>" WITH NO 
000205             ADVANCING.
000210     ACCEPT  IN-DATA.
000220     PERFORM TEST BEFORE
000230             VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 26
000240*----------------------------------------------------------------------
000250* COUNTER is a subscript used to compare the input character.
000260* The subscript is specified after the qualification.
000280*----------------------------------------------------------------------
000290       IF IN-DATA = CHAR OF UPPER-CASE (COUNTER) THEN
000300*----------------------------------------------------------------------
000310         EXIT PERFORM
000320       END-IF
000330     END-PERFORM.
000340     IF COUNTER <= 26 THEN
000350        DISPLAY "The lowercase letter corresponding to " IN-DATA " is "
000360               CHAR OF LOWER-CASE (COUNTER)"."
000370     ELSE
000380        DISPLAY "The input character is incorrect."
000390     END-IF.
000400 END PROGRAM SAMPLE.