In COBOL, tables (also known as arrays) are used to store multiple occurrences of data items of the same type. This is particularly useful when dealing with repetitive data structures, such as lists of names, account numbers, or transaction records. This section will cover the basics of defining, initializing, and manipulating tables and arrays in COBOL.

Key Concepts

  1. Table Definition: How to define a table in COBOL using the OCCURS clause.
  2. Table Initialization: Methods to initialize tables.
  3. Accessing Table Elements: Techniques to access and manipulate individual elements in a table.
  4. Multi-dimensional Arrays: Introduction to tables with more than one dimension.

Table Definition

Tables in COBOL are defined using the OCCURS clause within the DATA DIVISION. The OCCURS clause specifies the number of times a data item is repeated.

Example

DATA DIVISION.
WORKING-STORAGE SECTION.
01  STUDENT-TABLE.
    05  STUDENT-RECORD OCCURS 10 TIMES.
        10  STUDENT-ID    PIC 9(5).
        10  STUDENT-NAME  PIC X(20).
        10  STUDENT-GRADE PIC 9(2).

In this example:

  • STUDENT-TABLE is a table that can hold 10 student records.
  • Each STUDENT-RECORD consists of three fields: STUDENT-ID, STUDENT-NAME, and STUDENT-GRADE.

Table Initialization

Tables can be initialized in several ways, including using the INITIALIZE statement or manually setting each element.

Example

PROCEDURE DIVISION.
INITIALIZE STUDENT-TABLE.

This statement sets all elements of STUDENT-TABLE to their default values (numeric fields to zero and alphanumeric fields to spaces).

Alternatively, you can manually initialize each element:

MOVE 12345 TO STUDENT-ID (1).
MOVE 'John Doe' TO STUDENT-NAME (1).
MOVE 85 TO STUDENT-GRADE (1).

Accessing Table Elements

To access or modify elements in a table, you use the subscript notation. Subscripts in COBOL are 1-based.

Example

MOVE 12345 TO STUDENT-ID (1).
MOVE 'John Doe' TO STUDENT-NAME (1).
MOVE 85 TO STUDENT-GRADE (1).

DISPLAY 'Student ID: ' STUDENT-ID (1).
DISPLAY 'Student Name: ' STUDENT-NAME (1).
DISPLAY 'Student Grade: ' STUDENT-GRADE (1).

Multi-dimensional Arrays

COBOL also supports multi-dimensional arrays, which are defined by using multiple OCCURS clauses.

Example

DATA DIVISION.
WORKING-STORAGE SECTION.
01  SALES-TABLE.
    05  REGION OCCURS 5 TIMES.
        10  MONTHLY-SALES OCCURS 12 TIMES PIC 9(6)V99.

In this example:

  • SALES-TABLE is a two-dimensional array.
  • REGION occurs 5 times, and each REGION has 12 MONTHLY-SALES entries.

Accessing Multi-dimensional Arrays

To access elements in a multi-dimensional array, you use multiple subscripts.

Example

MOVE 1000.50 TO MONTHLY-SALES (1, 1).
DISPLAY 'Sales for Region 1, Month 1: ' MONTHLY-SALES (1, 1).

Practical Exercise

Exercise

  1. Define a table to store information about 5 employees. Each employee has an ID, name, and salary.
  2. Initialize the table with sample data.
  3. Write a COBOL program to display the information of all employees.

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. EmployeeTable.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  EMPLOYEE-TABLE.
    05  EMPLOYEE-RECORD OCCURS 5 TIMES.
        10  EMPLOYEE-ID    PIC 9(5).
        10  EMPLOYEE-NAME  PIC X(20).
        10  EMPLOYEE-SALARY PIC 9(7)V99.

PROCEDURE DIVISION.
    MOVE 10001 TO EMPLOYEE-ID (1).
    MOVE 'Alice Johnson' TO EMPLOYEE-NAME (1).
    MOVE 50000.00 TO EMPLOYEE-SALARY (1).

    MOVE 10002 TO EMPLOYEE-ID (2).
    MOVE 'Bob Smith' TO EMPLOYEE-NAME (2).
    MOVE 55000.00 TO EMPLOYEE-SALARY (2).

    MOVE 10003 TO EMPLOYEE-ID (3).
    MOVE 'Charlie Brown' TO EMPLOYEE-NAME (3).
    MOVE 60000.00 TO EMPLOYEE-SALARY (3).

    MOVE 10004 TO EMPLOYEE-ID (4).
    MOVE 'Diana Prince' TO EMPLOYEE-NAME (4).
    MOVE 65000.00 TO EMPLOYEE-SALARY (4).

    MOVE 10005 TO EMPLOYEE-ID (5).
    MOVE 'Eve Adams' TO EMPLOYEE-NAME (5).
    MOVE 70000.00 TO EMPLOYEE-SALARY (5).

    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
        DISPLAY 'Employee ID: ' EMPLOYEE-ID (I)
        DISPLAY 'Employee Name: ' EMPLOYEE-NAME (I)
        DISPLAY 'Employee Salary: ' EMPLOYEE-SALARY (I)
    END-PERFORM.

    STOP RUN.

Common Mistakes and Tips

  • Subscript Errors: Ensure that subscripts are within the defined range. COBOL subscripts are 1-based, not 0-based.
  • Initialization: Always initialize tables before use to avoid unexpected results.
  • Performance: Be mindful of the size of tables, as large tables can impact performance.

Conclusion

In this section, you learned how to define, initialize, and manipulate tables and arrays in COBOL. These structures are essential for handling repetitive data efficiently. In the next section, we will explore multi-dimensional arrays in more detail, providing you with the tools to handle more complex data structures.

© Copyright 2024. All rights reserved