COBOL multi dimensional array logic real approach to use in your project

COBOL array

You can use 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 single dimensional array and multi dimensional array in your COBOL project I will share my ideas.

One Dimensional Array Logic

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 one-dimensional array.

Two Dimensional Array Logic

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 multi-dimensional array.

Arrays

How You Can use Logic

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 dimens arrays you can define in COBOL program. Using perform statement you can read table data. Each OCCURS you need one PERFORM statement to read data.

Also Read

Shopping

Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Comments are closed.