MODULE 7: COBOL Control statements
PERFORM for Multi-dimensional arrays
- Also referred to as "PERFORM with VARYING AFTER"
- This kind of PERFORM browse through each elements of multi-dimensional array. For this purpose, we can use PERFORM in below specified format (below is simplified syntax for 2-dimentional array look up)
- Basic syntax:-
PERFORM [para-1 [{THRU/THROUGH} para-n]]
VARYING id-1 FROM id-2
BY id-3 UNTIL condition-1
AFTER VARYING id-4 FROM id-5
BY id-6 UNTIL condition-2
[statement-block END-PERFORM]
Where,
- para-1, para-n are paragraph names which we want to call
- id-1,id-4 can be numeric data item or index data item
- id-2,id-5 can be numeric data item or index data item or literal
- id-3,id-6 can be numeric data item or literal
- condition-1, condition-2 is any conditional-expression which can return either true or false
- [statement-block END-PERFORM] is used for Inline PERFORM.
- Example:-
Suppose we have data stored in our array (table) in below format
In DATA DIVISION,
01 DEPARTMENT-TABLE.
05 DEPARTMENT OCCCURS 3 TIMES.
10 DEPT-NAME PIC X(20).
10 EMP-NAME PIC X(20) OCCURS 2 TIMES.
Visually it will have data like:-
Reference visual:-
|
DEPT-NAME(1)
|
EMP-NAME(1,1)
|
EMP-NAME(1,2)
|
|
DEPT-NAME(2)
|
EMP-NAME(2,1)
|
EMP-NAME(2,2)
|
|
DEPT-NAME(3)
|
EMP-NAME(3,1)
|
EMP-NAME(3,2)
|
Values visual:-
|
MANUFACTURING
|
RAMESH
|
AKSHAY
|
|
QUALITY
|
STEVE
|
JOY
|
|
MARKETING
|
ASHWANI
|
JEFF
|
In PROCUEDURE DIVISION, we can write below code to display each elements of above multi-dimensional table
PARA-0.
PERFORM PARA-1 THRU PARA-1-EXIT
VARYING DEPT-I FROM 1 BY 1
UNTIL DEPT-I > 3
AFTER VARYING DEPT-J 1 BY 1
UNTIL DEPT-J > 2.
STOP RUN.
PARA-1.
DISPLAY ‘DEPARTMENT: ’ DEPT-NAME(DEPT-I)
DISPLAY ‘EMP-NAME: ’ EMP-NAME(DEPT-I, DEPT-J)
PARA-1-EXIT.
EXIT.
SYSOUT display (output):-
DEPARTMENT: MANUFACURING
EMP-NAME: RAMESH
DEPARTMENT: MANUFACURING
EMP-NAME: AKSHAY
DEPARTMENT: QUALITY
EMP-NAME: STEVE
DEPARTMENT: QUALITY
EMP-NAME: JOY
DEPARTMENT: MARKETING
EMP-NAME: ASHWANI
DEPARTMENT: MARKETING
EMP-NAME: JEFF
- You will be able to understand how the program has processed in above example by mapping reference visuals, values visuals and SYSOUT displays.