You can use a single dimension, two dimension and three dimension arrays in COBOL. You can give up to seven-dimensional arrays in theory. More than three-dimensional arrays will give confusion to you.
How to use a single-dimensional array and multi dimensional array in your COBOL project I will share my ideas.
One Dimensional Array Logic – An example
01 BangSalesRec. 88 EndOfSalesFile VALUE HIGH-VALUES. 02 BranchId PIC 9(7). 02 StateNum PIC 99. 02 SalesForMonth PIC 9(5)V99 OCCURS 12 TIMES.
One OCCURS clause is given in the above logic. So this kind of array is called a one-dimensional array.
Two Dimensional Array Logic – An example
01 StateSalesTable. 02 State OCCURS 50 TIMES. 03 StateBranchCount PIC 9(5). 03 StateMonthSales PIC 9(5)V99 OCCURS 12 TIMES.
There are two arrays in the above logic. One is STATE is 50 times and StateMonthSales is 12 times. So state wise, for 12 months sales you can store in this array. More than one OCCURS clause, when you see, then you can say it as a multi-dimensional array.
How to use Logic in Your Program Explained
01 BranchSalesRec. 88 EndOfSalesFile VALUE HIGH-VALUES. 02 BranchId PIC 9(7). 02 StateNum PIC 99. 02 SalesForMonth PIC 9(5)V99 OCCURS 12 TIMES. WORKING-STORAGE SECTION. 01 StateSalesTable. 02 State OCCURS 50 TIMES. 03 StateBranchCount PIC 9(5). 03 StateMonthSales PIC 9(5)V99 OCCURS 12 TIMES. ... ... PERFORM VARYING StateIdx FROM 1 BY 1 UNTIL StateIdx GREATER THAN 50 MOVE StateIdx TO PrnStateNum MOVE StateBranchCount(StateIdx) TO PrnBranchCount PERFORM VARYING MonthIdx FROM 1 BY 1 UNTIL MonthIdx > 6 MOVE StateMonthSales(StateIdx, MonthIdx) TO PrnMonthSales1(MonthIdx) END-PERFORM PERFORM VARYING MonthIdx FROM 7 BY 1 UNTIL MonthIdx > 12 MOVE StateMonthSales(StateIdx, MonthIdx) TO PrnMonthSales2(MonthIdx - 6) END-PERFORM DISPLAY DetailLine1 DISPLAY DetailLine2 DISPLAY SPACES END-PERFORM
The Bottom Line
Multi dimensional array and single dimension arrays you can define in the COBOL program. Using a perform statement you can read table data. Each OCCURS you need one PERFORM statement to read data.
- 3 GO TO Statements in COBOL absolutely to Avoid now
- COBOL multi dimensional array logic real approach to use in your project
- COBOL Indexed Vs Sort top differences you need to know