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
- Definition of Arrays: Arrays in REXX are associative arrays, also known as stem variables.
- Creating Arrays: How to declare and initialize arrays.
- Accessing Array Elements: How to read and write array elements.
- Iterating Over Arrays: Techniques for looping through array elements.
- Multi-dimensional Arrays: Handling arrays with more than one dimension.
- 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.
- 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.
- 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 */
- Iterating Over Arrays
To iterate over the elements of an array, you can use a DO
loop. Here’s an example:
This loop will output each element of the array:
- 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
- Create an array named
fruits
with the following elements: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'. - Print each element of the array.
- Add a new element 'Fig' to the array.
- 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.
REXX Programming Course
Module 1: Introduction to REXX
- What is REXX?
- Setting Up the REXX Environment
- Hello World in REXX
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Basic Programming Concepts
- Operators and Expressions
- Control Structures: IF/THEN/ELSE
- Loops: DO and LEAVE
- Input and Output
- Basic String Manipulation
Module 3: Intermediate REXX Programming
Module 4: Advanced REXX Programming
- Advanced String Manipulation
- Parsing Techniques
- Interfacing with External Programs
- REXX Macros
- Performance Optimization