Arrays in any programming language allow you to group a list of
values of the same type. The use of arrays is that it allows you to
build a list 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 initialized. 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.
The general syntax for creating arrays is as follows −
Example
The following program is an example of values which can be assigned to various index values of an existing array.
Example
Example
Example
Example
Rexx arrays may be sparse. That is, not every array position must have a value or even be initialized. 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 = valuewhere
- 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.
Example
/* Main program */ list.1 = 0 list.2 = 0 list.3 = 0The following points needs to be noted about the above program −
- The name of the array is given as list
- There are 3 elements of the array which are initialized 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 initialized.The following program is an example of values which can be assigned to various index values of an existing array.
/* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.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 */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.3 = 30 say list.1 say list.2 say list.3The 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.
Example
/* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.3 = 30 listnew. = list. say listnew.1 say listnew.2 say listnew.3The 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 */ list.1 = 10 list.2 = 20 list.3 = 30 number_of_elements = 3 do j = 1 to number_of_elements say list.j endThe 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.
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 */ list.1 = 10 list.1.1 = 11 list.1.2 = 12 say list.1 say list.1.1 say list.1.2The output of the above program will be shown as follows −
10 11 12The 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 list.1.1 to create another inner array for the index value 1 of the list array.
No comments:
Post a Comment