Open In App

Array or Table Processing in COBOL

Last Updated : 19 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Arrays comes under the processing of “Linear data structure”. It is a collection of single data items that can be referred by a single data name. Data items contained in an array are called as its elements. Elements of an array are internally stored in contiguous memory locations To access elements of the array, will use INDEX or SUBSCRIPT 

Declaration of the Tables in COBOL:

The declaration is similar to any other data item an array is defined in the DATA DIVISION. To specify the repeated occurrence of the data item with the same format, the OCCURS clause can be used only with levels number 02 to 49. Using OCCURS clause we can specify the maximum number of elements that can be stored in the array

Subscript in Table:

In the array/table we can access the element of an array by using SUBSCRIPT. The subscript can be a positive integer. The subscript must be enclosed within a pair of parenthesis. The highest value that the subscript can take is the integer value specified in the OCCURS clause. The lowest value is implicitly assumed to be 1.

Index in Table:

We use the INDEX BY clause to initialize the value. We won’t define the index by clause in the working-storage section. To modify the value of an index the SET verb is used. It will represent a displacement from the address of the first element

Example 1:

Cobol




One Dimensional
IDENTIFICATION DIVISION.
PROGRAM-ID. ONE-DIMENSIONAL.
DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 COUNTS PIC 9(02) VALUE 0.
    01 MONTHS VALUE
        "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC".
            05 MONTH OCCURS 12 TIMES PIC A(3).
PROCEDURE DIVISION.
    PERFORM VARYING COUNTS FROM 1 BY 1 UNTIL COUNTS=13
    DISPLAY "MONTHS VALUE:  " MONTH(COUNTS)
    END-PERFORM.
STOP RUN.


Output:

 

Example 2:

Cobol




2D Array
IDENTIFICATION DIVISION.
PROGRAM-ID. TWO-DIMENSIONAL.
DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 EMP-DATAS VALUE 'BAT RAT CAT MAT'.
        05 WS-GROUP OCCURS 2 TIMES INDEXED BY I.
            10 WS-NAME OCCURS 2 TIMES INDEXED BY J PIC X(4).
PROCEDURE DIVISION.
1000-MAIN-PARA.
    PERFORM 2000-DISP-PARA VARYING I FROM 1 BY 1 UNTIL I>2
        AFTER J FROM 1 BY 1 UNTIL J>2
    STOP RUN.
2000-DISP-PARA.
    DISPLAY "TABLE VALUE:  " WS-NAME(I,J).


Output:

 

Set Statement in Table:

  • It is used to modify the index value

Syntax:

SET index-1 , . . . .  To { index-2, identifier-2, integer-1 }.

Or

SET index-1, . . . . { UP BY, DOWN BY } { integer, identifier  }.

Example 3:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. ARRAYS.
DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 DAYS.
        05 WS-DAYS OCCURS 3 TIMES INDEXED BY I.
            10 WS-NUM PIC 9(05).
    01 COUNTS PIC 9(02) VALUE 1.
PROCEDURE DIVISION.
1000-MAIN-PARA.
    PERFORM 2000-DISP-PARA UNTIL COUNTS > 3
    STOP RUN.
2000-DISP-PARA.  
    MOVE COUNTS TO WS-NUM(I).
    DISPLAY "TABLE VALUE:  " WS-NUM(I).
    ADD 1 TO COUNTS.
    SET I UP BY 1.


Output:

 

Search in Table :

The SEARCH keyword is used to check for the presence or absence of any particular elements in any tables or arrays. We can find an element by using the SEARCH keyword. It performs the Linear search using table names as indexes.

Syntax:

SEARCH table-name [ VARYING index ]

[AT END imperative statement-1 ]

{ WHEN condition-1 {statement-2, NEXT SENTENCE } }                        

[ END- SEARCH ].

Search All in Table : 

Same to the search in the COBOL, SEARCH ALL is used to find the presence of any particular element in the table. Unlike the search, it won’t perform a Linear search. Search All will work like “Binary Search” using table names as indexes.

Syntax:

SEARCH ALL table-name [ VARYING index ]

[AT END imperative statement-1 ]

{ WHEN condition-1 {statement-2, NEXT SENTENCE } }                        

[ END- SEARCH ].

Example 4:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A PIC 9(1) OCCURS 10 TIMES INDEXED BY I.
01 WS-SRCH PIC 9(1) VALUE '0'.
 
PROCEDURE DIVISION.
   MOVE 'GeeksforGeeks' TO WS-TABLE.
   SET I TO 1.
   SEARCH WS-A
      AT END DISPLAY 'e is found in a table'
      WHEN WS-A(I) = WS-SRCH
      DISPLAY 'e is not found in a table'
   END-SEARCH.
   STOP RUN.


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads