You can use single dimension, two dimensional, and three-dimension arrays in COBOL. You can give up to seven-dimensional-arrays in theory. More than three-dimensional arrays cause your code hard to read.
How to use a single-dimensional array and multi dimensional array in your COBOL project I will share my ideas.
1. 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.
2. 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.
3. 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.
One thought
Comments are closed.