Group Items and Practical Data Handling

Introducing Group Items and Practical Data Handling

In our previous lessons, we delved into foundational COBOL elements, understanding the basic data structures and how to manipulate them. Now, we are ready to take a step further. This lesson will focus on Group Items and Practical Data Handling. Group Items are crucial for managing complex data structures effectively in COBOL.

What You'll Learn

In this lesson, we'll explore how to define and manipulate group items in COBOL. Group items allow you to handle related data as a single unit, making your programs more organized and easier to manage. By the end of this lesson, you will know how to:

  1. Define group items in the DATA DIVISION.
  2. Move data to individual elements within a group.
  3. Display the contents of group items.

Here's a simple example to get us started:

IDENTIFICATION DIVISION.
PROGRAM-ID. GroupItemDemo.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Customer.
    05 Customer-Name PIC X(30).
    05 Account-Number PIC 9(10).

PROCEDURE DIVISION.
    MOVE "Jane Smith" TO Customer-Name.
    MOVE 9876543210 TO Account-Number.

    DISPLAY "Customer Name: " Customer-Name.
    DISPLAY "Account Number: " Account-Number.

    DISPLAY Customer.
    DISPLAY Customer-Name OF Customer.
    STOP RUN.

Breaking Down the Example

In this example, we define a group item, Customer that contains two elements:

  • Customer-Name (a 30-character alphanumeric field)
  • Account-Number (a 10-digit numeric field)

Notice how we define the group item Customer using level 01 and its child items using level 05. This hierarchical structure helps organize related data fields. For nested fields, we can use other level values up to 49, which is the lowest level of the hierarchy, but in COBOL, it is a common practice to use levels 01, 05, 10, etc., for better readability.

In the PROCEDURE DIVISION, we assign values to Customer-Name and Account-Number using the MOVE statement.

Finally, we display the entire Customer group item and the Customer-Name field within the Customer group. This demonstrates how you can access and display group items and their individual elements. Notice that we use the OF keyword to access the individual elements of a group item in the DISPLAY statement.

When you run this program, you'll see the following output:

Jane Smith                    9876543210
Jane Smith
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal