Lesson 1

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:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. GroupItemDemo. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Customer. 6 05 Customer-Name PIC X(30). 7 05 Account-Number PIC 9(10). 8 9PROCEDURE DIVISION. 10 MOVE "Jane Smith" TO Customer-Name. 11 MOVE 9876543210 TO Account-Number. 12 13 DISPLAY "Customer Name: " Customer-Name. 14 DISPLAY "Account Number: " Account-Number. 15 16 DISPLAY Customer. 17 DISPLAY Customer-Name OF Customer. 18 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 another level values up to 49 which is the lowest level of the hierarchy, but in COBOL it is a common practice to increment levels by 5.

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:

1Jane Smith 9876543210 2Jane Smith
Practical Applications

Understanding how to handle group items is essential in real-world programming. Imagine you are managing customer data for a bank. Each customer has a name and an account number, among other details. Grouping these related pieces of information allows you to manage them more efficiently.

In the example above, Customer is a group item containing attributes like Customer-Name and Account-Number. This structured approach simplifies data handling and improves code readability.

After this lesson, you'll be able to apply these concepts to various scenarios, enhancing your ability to structure and manage complex data in COBOL.

Are you ready to dive in? Let's begin the practice section and solidify these concepts together.

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.