Previous Page | Next Page

SCL Arrays

Resizing Dynamic Arrays in SCL Programs

You can use the REDIM function to explicitly change the high bound of any dimension of a dynamic array at runtime. You can use the REDIMOPT function to mark an array as implicitly growable.


Explicitly Resizing An Array With REDIM

With REDIM, you cannot change the number of dimensions or type of the array, only the bounds. The REDIM function will also preserve the data in the array unless you resize the array to a smaller size. If you reduce the size of an array, you will lose the data in the eliminated elements.

For example, suppose that you have declared and initialized the STUDENTS array as shown in Dynamic STUDENTS Array. To add another student, you must resize the array. The following statements increase the high bound by 1 element, add the new variable STUDENTS[4], and initialize this new element to Alice.

declare num rc;
rc = REDIM(students, DIM(students) + 1);
students[DIM(students)] = 'Alice';
put students;

All of the existing data is preserved. The low bound for the array STUDENTS is 1, and the new high bound is 4. The output for this example would be:

students[1] = 'Mary'
students[2] = 'Johnny'
students[3] = 'Bobby'
students[4] = 'Alice'

You can also use the REDIM function to create and initialize an array that has been declared but not yet created by other means. For example, the following statements declare, create, and initialize an array of five elements to numeric missing values:

dcl num rc;
dcl num a[*];
rc = redim(a,5);

There is no limit to the number of times that you can resize an array.

Note:   You can use the MAKEARRAY function to resize an array, but all the data will be lost. The MAKEARRAY function will reinitialize the elements to missing numeric values or to blank character values.   [cautionend]

Previous Page | Next Page | Top of Page