English 中文(简体)
Rexx - Arrays
  • 时间:2024-12-22

Rexx - Arrays


Previous Page Next Page  

Arrays in any programming language allow you to group a pst of values of the same type. The use of arrays is that it allows you to build a pst of similar type of values which are sortable, searchable and can be easily manipulated. Rexx also allows one to define arrays. These arrays can be one dimensional or multidimensional.

Rexx arrays may be sparse. That is, not every array position must have a value or even be initiapzed. There can be empty array positions, or slots, between those that do contain data elements. Or arrays can be dense, in which consecutive array slots all contain data elements.

In many programming languages, you must be concerned with what the subscript of the first entry in a table is. Is the first numeric subscript 0 or 1? In Rexx, the first subscript is whatever you use! So, input the first array element into position 0 or 1 as you prefer.

array_name.0 = ‘first element’

or

array_name.1 = ‘first element’

Let’s look at the different operations available for arrays.

Creating Arrays

Arrays are created with the same naming convention which is used for variables in Rexx.

The general syntax for creating arrays is as follows −

Arrayname.index = value 

where

    Arrayname − This is the name provided to the array.

    Index − This is the index position in the array to refer to a specific element.

    Value − This is the value assigned to the index element in the array.

An example of an array declaration is as follows −

Example

/* Main program */ 
pst.1 = 0 
pst.2 = 0 
pst.3 = 0

The following points needs to be noted about the above program −

    The name of the array is given as pst

    There are 3 elements of the array which are initiapzed to the value of 0.

Assigning Values to an Array Element

Values can be re-assigned to array elements in the same way as array elements are initiapzed.

The following program is an example of values which can be assigned to various index values of an existing array.

/* Main program */ 
pst.1 = 0 
pst.2 = 0 
pst.3 = 0 

/* Assigning new values to the array*/ 
pst.1 = 10 
pst.3 = 30 

Displaying Values of an Array

The values of an array can be displayed by referring to the index position of the array element. The following example shows to access various elements of the array.

Example

/* Main program */ 
pst.1 = 0 
pst.2 = 0 
pst.3 = 0 

/* Assigning new values to the array*/ 
pst.1 = 10 
pst.3 = 30 
say pst.1 
say pst.2 
say pst.3 

The output of the above program will be as follows −

10
0
30

Copying Arrays

All of the elements of an array can be copied onto another array. The general syntax of this is as follows −

Newarray. = sourcearray. 

where

    Newarray − This is the new array in which the elements need to be copied onto.

    Sourcearray − This is the source array from which the elements need to be copied.

An example on how the copy operations for arrays can be carried out is shown in the following program −

Example

/* Main program */ 
pst.1 = 0 
pst.2 = 0 
pst.3 = 0 

/* Assigning new values to the array*/ 
pst.1 = 10 
pst.3 = 30 
pstnew. = pst. 

say pstnew.1 
say pstnew.2 
say pstnew.3 

The output of the above program will be −

10
0
30

Iterating through array elements

Elements of an array can also be iterated by using the iterative statements available in Rexx. An example on how this can be done is as follows −

Example

/* Main program */ 
pst.1 = 10 
pst.2 = 20 
pst.3 = 30 

number_of_elements = 3 
do j = 1 to number_of_elements 
say pst.j 
end 

The following pointers need to be noted about the above program −

    The do loop is used to iterate through the array elements.

    The variable number_of_elements is used to store the number of elements in the array.

    The variable j is used to iterate through each element of the array.

The output of the above program will be −

10
20
30

Two-dimensional Arrays

It was also mentioned that we can construct multi-dimensional arrays in Rexx. Let’s look at an example of how we can implement a 2-dimensional array.

Example

/* Main program */ 
pst.1 = 10 
pst.1.1 = 11 
pst.1.2 = 12 

say pst.1 
say pst.1.1 
say pst.1.2 

The output of the above program will be shown as follows −

10
11
12

The following point needs to be noted about the above program −

    To create a multidimensional array, we can use another layer of indexing. So in our example, we used pst.1.1 to create another inner array for the index value 1 of the pst array.

Advertisements