In this section, we will explore arrays and lists in RPG programming. Arrays and lists are fundamental data structures that allow you to store and manipulate collections of data efficiently. Understanding how to use these structures is crucial for handling multiple data items in your programs.

Key Concepts

  1. Arrays:

    • Fixed-size data structures.
    • Store elements of the same data type.
    • Indexed by integers.
  2. Lists:

    • Dynamic-size data structures.
    • Can grow and shrink as needed.
    • Typically implemented using pointers or linked structures.

Arrays

Declaring Arrays

In RPG, arrays are declared using the D specification. Here is the basic syntax for declaring an array:

D arrayName      S              10A   DIM(5)
  • arrayName: The name of the array.
  • S: Indicates a standalone field.
  • 10A: Specifies the data type and length (10 characters in this case).
  • DIM(5): Specifies the dimension of the array (5 elements).

Initializing Arrays

You can initialize arrays at the time of declaration or later in the code. Here is an example of both:

D arrayName      S              10A   DIM(5) INZ('A' : 'B' : 'C' : 'D' : 'E')

/* Later initialization */
arrayName(1) = 'A';
arrayName(2) = 'B';
arrayName(3) = 'C';
arrayName(4) = 'D';
arrayName(5) = 'E';

Accessing Array Elements

Array elements are accessed using their index. The index starts from 1 in RPG:

D arrayName      S              10A   DIM(5)
arrayName(1) = 'First Element';
arrayName(2) = 'Second Element';

Iterating Over Arrays

You can use a loop to iterate over the elements of an array:

D arrayName      S              10A   DIM(5) INZ('A' : 'B' : 'C' : 'D' : 'E')
D i              S              10I 0

For i = 1 to %elem(arrayName);
    dsply arrayName(i);
EndFor;

Lists

Declaring Lists

Lists in RPG can be implemented using data structures and pointers. Here is an example of a simple linked list:

D ListNode       DS                  BASED(pListNode)
D  value                       10A
D  next                        *    POINTER

D pListNode      S               *   INZ(*NULL)
D pCurrent       S               *   INZ(*NULL)
D pNewNode       S               *   INZ(*NULL)

Adding Elements to a List

To add elements to a list, you need to allocate memory for a new node and link it to the existing list:

pNewNode = %alloc(%size(ListNode));
pListNode = pNewNode;
pListNode->value = 'First Element';
pListNode->next = *NULL;

/* Adding another element */
pNewNode = %alloc(%size(ListNode));
pNewNode->value = 'Second Element';
pNewNode->next = *NULL;
pListNode->next = pNewNode;

Iterating Over a List

To iterate over a list, you follow the pointers from one node to the next:

pCurrent = pListNode;
DoW pCurrent <> *NULL;
    dsply pCurrent->value;
    pCurrent = pCurrent->next;
EndDo;

Practical Exercises

Exercise 1: Array Manipulation

  1. Declare an array of 10 integers.
  2. Initialize the array with values from 1 to 10.
  3. Write a loop to print each element of the array.

Solution:

D intArray       S             10I 0 DIM(10)
D i              S             10I 0

For i = 1 to 10;
    intArray(i) = i;
EndFor;

For i = 1 to 10;
    dsply intArray(i);
EndFor;

Exercise 2: Linked List Implementation

  1. Create a linked list to store strings.
  2. Add three elements to the list.
  3. Write a loop to print each element of the list.

Solution:

D ListNode       DS                  BASED(pListNode)
D  value                       10A
D  next                        *    POINTER

D pListNode      S               *   INZ(*NULL)
D pCurrent       S               *   INZ(*NULL)
D pNewNode       S               *   INZ(*NULL)

/* Adding first element */
pNewNode = %alloc(%size(ListNode));
pListNode = pNewNode;
pListNode->value = 'First';
pListNode->next = *NULL;

/* Adding second element */
pNewNode = %alloc(%size(ListNode));
pNewNode->value = 'Second';
pNewNode->next = *NULL;
pListNode->next = pNewNode;

/* Adding third element */
pNewNode = %alloc(%size(ListNode));
pNewNode->value = 'Third';
pNewNode->next = *NULL;
pListNode->next->next = pNewNode;

/* Iterating over the list */
pCurrent = pListNode;
DoW pCurrent <> *NULL;
    dsply pCurrent->value;
    pCurrent = pCurrent->next;
EndDo;

Summary

In this section, we covered the basics of arrays and lists in RPG programming. We learned how to declare, initialize, and manipulate arrays, as well as how to implement and traverse linked lists. These data structures are essential for managing collections of data efficiently in your programs. In the next module, we will delve into more advanced data handling techniques, including file handling and database access.

© Copyright 2024. All rights reserved