Lesson 3

Handling Two Group Items

Introduction to Handling Two Group Items

Welcome back to our deep dive into advanced COBOL data management! In the previous lessons, we've mastered the basics of group items and performed simple arithmetic operations within them. Now, let's push the boundaries further by handling multiple group items within a COBOL program. This skill is critical for scenarios where you need to manage and manipulate diverse sets of data concurrently.

What You'll Learn

In this lesson, you will learn how to:

  1. Define multiple group items in the Data Division.
  2. Move values between these group items.
  3. Display data from different group items effectively.

For instance, you will be able to write and understand code similar to the following by the end of this lesson:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. MoreGroupItems. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Account. 6 05 Account-User-Name PIC X(20). 7 05 Account-Balance PIC 9(6) VALUE 0. 8 05 Account-Status PIC X VALUE 'A'. 9 1001 Support. 11 05 Support-User-Name PIC X(20). 12 05 Counter PIC 9(2) VALUE 0. 13 05 Account-Status PIC X VALUE 'D'. 14 15PROCEDURE DIVISION. 16 MOVE "John" TO Account-User-Name. 17 MOVE "Alice" TO Support-User-Name. 18 19 MOVE 'I' TO Account-Status OF Support. 20 21 DISPLAY "Account User Name: " Account-User-Name. *> John 22 DISPLAY "Support User Name: " Support-User-Name. *> Alice 23 DISPLAY Account-Status OF Account. *> A 24 DISPLAY Account-Status OF Support. *> I 25 26 STOP RUN.

In our code snippet above, we have two group items: Account and Support. Within the Account group, we manage user-specific information like Account-User-Name and Account-Balance. Similarly, the Support group helps us track support-related details. By moving values and displaying data appropriately, you can keep different types of data organized and accessible.

Notice that the Account-Status field is present in both group items. This demonstrates how you can use the same field name in multiple group items without conflicts. This flexibility allows you to reuse field names across different group items, enhancing code readability and maintainability. We use the OF phrase to specify the group item to which a field belongs when assigning values or displaying it.

Now, let's examine how we can move the value of Account-Status from the Account group to the Support group and display it. We leverage the OF operator to specify the group item from which we want to move the value. Here's how you can do it:

cobol
1 MOVE Account-Status OF Account TO Account-Status OF Support. 2 3 DISPLAY Account-Status OF Account. *> A 4 DISPLAY Account-Status OF Support. *> A
Why It Matters

The ability to handle multiple group items gracefully is a vital skill in COBOL programming. It enhances your data management capabilities, allowing you to build more sophisticated and efficient applications. By mastering this lesson, you will be better equipped to tackle complex data structures and interactions, thereby improving the quality and functionality of your programs.

Excited to take your COBOL skills to the next level? Let’s head into the practice section and start working with multiple group items together!

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

Practice is how you turn knowledge into actual skills.