COBOL - Subscript Clause
Use the data-name of the table element, along with its occurrence number (called a subscript) in parentheses. This technique is called subscripting.
- Table individual elements can be accessed by using subscript.
- Subscript values can range from 1 to the number of times the table occurs. A subscript can be any positive number.
- It does not require any declaration in data division. It is automatically created with occurs clause.
Example 1:
01 ANY-TABLE.
05 TABLE-ELEMENT PIC X(10) OCCURS 3 TIMES VALUE "SRINIVASAN". |
........
MOVE "XX" TO TABLE-ELEMENT (1) (3 : 2). |
The MOVE statement in the example above moves the string 'XX' into table element number 1, beginning at character position 3, for a length of 2 characters.
ANY-TABLE(before change) | ANY-TABLE(after change) |
SRINIVASAN | SRXXIVASAN |
SRINIVASAN | SRINIVASAN |
SRINIVASAN | SRINIVASAN |
Example 2:
01 WS-TABLE.
05 WS-A OCCURS 3 TIMES.
10 WS-B PIC A(2).
10 WS-C OCCURS 2 TIMES.
15 WS-D PIC X(3).
|
........
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
DISPLAY 'WS-TABLE : ' WS-TABLE.
DISPLAY 'WS-A(1) : ' WS-A(1).
DISPLAY 'WS-C(1,1) : ' WS-C(1,1).
DISPLAY 'WS-C(1,2) : ' WS-C(1,2).
DISPLAY 'WS-A(2) : ' WS-A(2).
DISPLAY 'WS-C(2,1) : ' WS-C(2,1).
DISPLAY 'WS-C(2,2) : ' WS-C(2,2).
DISPLAY 'WS-A(3) : ' WS-A(3).
DISPLAY 'WS-C(3,1) : ' WS-C(3,1).
DISPLAY 'WS-C(3,2) : ' WS-C(3,2).
|
Output:
WS-TABLE : 12ABCDEF34GHIJKL56MNOPQR
WS-A(1) : 12ABCDEF
WS-C(1,1) : ABC
WS-C(1,2) : DEF
WS-A(2) : 34GHIJKL
WS-C(2,1) : GHI
WS-C(2,2) : JKL
WS-A(3) : 56MNOPQR
WS-C(3,1) : MNO
WS-C(3,2) : PQR
|
Table elements can also be accessed using index.
Next chapter going to discuss about Index.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!