Working with Arrays and Tables in COBOL

May 4, 2024

Arrays and tables are fundamental data structures in COBOL programming, allowing you to store and manipulate multiple values efficiently. In this post, we will explore how to work with arrays and tables in COBOL, covering initialization, manipulation, and retrieval of data.

Working with Arrays in COBOL

In COBOL, arrays are defined using OCCURS clause within the data division. Here’s an example of how to declare and initialize an array:

01 MY-ARRAY OCCURS 10 TIMES.
   05 ARRAY-ELEMENT PIC X(10) VALUE 'INITIAL'.

To access elements of the array, you can use index notation. For example, to access the third element of MY-ARRAY:

MOVE 'UPDATED' TO ARRAY-ELEMENT(3).

Working with Tables in COBOL

Tables in COBOL are similar to arrays but offer more flexibility in terms of size and structure. Here’s an example of how to declare and initialize a table:

01 MY-TABLE.
   05 TABLE-ELEMENT OCCURS 10 TIMES.
      10 NAME PIC X(20).
      10 AGE PIC 9(3).

You can access elements of the table using index notation as well. For instance, to retrieve the name and age of the fifth element:

MOVE 'John Doe' TO NAME(5).
MOVE 30 TO AGE(5).

Manipulating arrays and tables in COBOL involves using various verbs such as MOVE, ADD, SUBTRACT, MULTIPLY, and DIVIDE. These verbs allow you to perform arithmetic and data manipulation operations on array elements.

By understanding how to work with arrays and tables in COBOL, you can effectively manage and process large sets of data in your programs. Practice implementing arrays and tables in your COBOL code to enhance your programming skills.