In this section, we will explore how to work with arrays in REXX. Arrays are a fundamental data structure that allows you to store multiple values in a single variable. Understanding how to use arrays effectively can significantly enhance your ability to manage and manipulate data in your REXX programs.

Key Concepts

  1. Definition of Arrays: Arrays in REXX are associative arrays, also known as stem variables.
  2. Creating Arrays: How to declare and initialize arrays.
  3. Accessing Array Elements: How to read and write array elements.
  4. Iterating Over Arrays: Techniques for looping through array elements.
  5. Multi-dimensional Arrays: Handling arrays with more than one dimension.

  1. Definition of Arrays

In REXX, arrays are implemented as associative arrays, which means they are indexed by strings rather than just integers. This allows for more flexible and dynamic data structures.

  1. Creating Arrays

To create an array in REXX, you use stem variables. A stem variable is a variable that ends with a period (.). Here’s how you can declare and initialize an array:

/* Declare an array */
myArray. = ''

/* Initialize array elements */
myArray.1 = 'Apple'
myArray.2 = 'Banana'
myArray.3 = 'Cherry'

In this example, myArray. is the stem variable, and myArray.1, myArray.2, and myArray.3 are the elements of the array.

  1. Accessing Array Elements

You can access the elements of an array using their indices. Here’s an example:

/* Accessing array elements */
say myArray.1  /* Output: Apple */
say myArray.2  /* Output: Banana */
say myArray.3  /* Output: Cherry */

  1. Iterating Over Arrays

To iterate over the elements of an array, you can use a DO loop. Here’s an example:

/* Iterate over array elements */
do i = 1 to 3
    say myArray.i
end

This loop will output each element of the array:

Apple
Banana
Cherry

  1. Multi-dimensional Arrays

REXX also supports multi-dimensional arrays. You can create a multi-dimensional array by using multiple indices. Here’s an example:

/* Declare a 2D array */
my2DArray. = ''

/* Initialize 2D array elements */
my2DArray.1.1 = 'A1'
my2DArray.1.2 = 'A2'
my2DArray.2.1 = 'B1'
my2DArray.2.2 = 'B2'

/* Accessing 2D array elements */
say my2DArray.1.1  /* Output: A1 */
say my2DArray.2.2  /* Output: B2 */

Practical Exercise

Exercise 1: Create and Manipulate an Array

  1. Create an array named fruits with the following elements: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'.
  2. Print each element of the array.
  3. Add a new element 'Fig' to the array.
  4. Print the updated array.

Solution

/* Step 1: Create an array */
fruits. = ''
fruits.1 = 'Apple'
fruits.2 = 'Banana'
fruits.3 = 'Cherry'
fruits.4 = 'Date'
fruits.5 = 'Elderberry'

/* Step 2: Print each element */
do i = 1 to 5
    say fruits.i
end

/* Step 3: Add a new element */
fruits.6 = 'Fig'

/* Step 4: Print the updated array */
do i = 1 to 6
    say fruits.i
end

Common Mistakes and Tips

  • Uninitialized Elements: Ensure that all elements of the array are initialized before accessing them to avoid unexpected results.
  • Indexing: Remember that REXX arrays are 1-based, not 0-based.
  • Dynamic Indexing: Take advantage of REXX's associative arrays to use dynamic and meaningful indices.

Conclusion

In this section, we covered the basics of working with arrays in REXX, including how to create, access, and iterate over arrays, as well as handling multi-dimensional arrays. Arrays are a powerful tool for managing collections of data, and mastering their use will greatly enhance your REXX programming skills. In the next module, we will delve into more advanced topics such as advanced string manipulation and parsing techniques.

© Copyright 2024. All rights reserved