Rexx Programming/How to Rexx/array
The rexx scripting language supports associative containers.
What is an array?
[edit | edit source]An array is a variable containing several elements, and provides a convenient way of grouping a lot of values under a single variable name. An array is a bit like pigeon hole compartments with multiple storage places. One way to code arrays in Rexx is to use so-called "stem variables" which use periods.
/* Let's use a stem variable to store three color names. */ color.1 = "red" color.2 = "green" color.3 = "blue"
In the above code "color" is the name of the array; however, there are three color names, so each one gets stored with a number as well. The number is called an index. For example, the second color name (which is "green") gets stored with the index 2 in the stem variable "color.2". If we wanted to store a fourth color name, we would just give it a fourth index, such as the number 4.
color.4 = "magenta"
Every time you store a value in an array with a new index that hasn't been used before, Rexx will automatically set aside more of the computer's memory so that the array will be big enough. We could easily add a fifth color, sixth color, seventh color and so on. This is different to some other languages which require you to say how large the array will be ahead of time.
An array can hold multiple scalar values
[edit | edit source]An array contains sets of scalar values called elements. A variable that hold an array is called an array variable and can hold any number of elements and may be sparse or empty.
/* This first array has two elements. */ size.1 = "small" size.2 = "large" /* The next array has four elements, but the count starts at zero. */ flavor.0 = "vanilla" flavor.1 = "chocolate" flavor.2 = "strawberry" flavor.3 = "banana" /* We can make an array larger later by just assigning more values. */ flavor.4 = "blueberry" flavor.5 = "matcha" size.3 = "supersize"
It fairly common to fill arrays using loops, especially when the user needs to enter or view many pieces of data at once.
/* The user can enter as many names as will fit in memory. */ say "How many names do you want to enter?" pull quantity do count = 1 to quantity say "Enter a new name please:" pull name.count end say "Here are all the names you entered:" do count = 1 to quantity say name.count end if quantity > 0 then do say "The first name you entered was:" name.1 say "The last name you entered was:" name.quantity end
Arrays can be multidimensional
[edit | edit source]In rexx it is possible to use multidimensional arrays. This means that we need more than just one index to pinpoint a value in the array. One way to think of a multidimensional array is as an array made up of other arrays. Rather than being just a list of values in order, a multidimensional array is a list of lists.
/* One-dimensional array */ a.1 = "first string" a.2 = "second string" a.3 = "third string" /* Two-dimensional array */ b.1.1 = "first item of first list" b.1.2 = "second item of first list" b.2.1 = "first item of second list" b.2.2 = "second item of second list" /* Three dimensional array */ c.1.1.1 = "Maybe this represents the first room on the first floor of building 1?"
Two-dimensional arrays are often used to store tables of values in memory. For example, here is a script that creates a multiplication table using a two-dimensional array:
/* Arrange some products in rows and columns. */ do row = 0 to 12 do column = 0 to 12 table.row.column = column * row end end /* Let's look up a value in the table. */ say "3 times 7 equals" table.3.7
See also the example using 3 dimension array "A"
I = 1 J = 1 K = 1 N = 1 DO WHILE I < 6 DO WHILE J < 6 DO WHILE K < 6 A.I.J.K = N K = K + 1 N = N + 1 END K = 1 J = J + 1 END J = 1 I = I + 1 END
Pointing out one element for example
I=5 J=4 K=2 SAY A.3.4.1 /* pount out one array must have the value 66 in this example */
An array can be sparse
[edit | edit source]In rexx, an array can be sparse. This means that not every array position has to have a value or be initialized. There can be empty slots or positions within the array that do not contain data elements.
/* Judith lives in room 1, and Michael lives in room 2 */ resident.1 = "Judith" resident.2 = "Michael" /* Carlos lives in room 5. */ resident.5 = "Carlos" /* So far rooms 3 and 4 are empty. This is a sparse array. */