Show List

Arrays in CL400

In CL400, an array is a collection of elements of the same data type that can be accessed using an index. An array can be either one-dimensional or multi-dimensional, and it can hold up to the maximum number of elements allowed by the system. Here's how you can create and work with one-dimensional and multi-dimensional arrays in CL400:

  • Creating a One-Dimensional Array:

To create a one-dimensional array, you need to use the DCL statement, followed by the VAR keyword, the array name, the TYPE keyword, the data type of the array elements, and the LEN keyword, the number of elements in the array. Here is an example:

scss
Copy code
DCL VAR(&MYARRAY) TYPE(*CHAR) LEN(5)

In this example, we have created a one-dimensional character array named &MYARRAY that can hold up to 5 elements. To initialize the array with values, you can use the following syntax:

scss
Copy code
DCL VAR(&MYARRAY) TYPE(*CHAR) LEN(5) VALUE('A' 'B' 'C' 'D' 'E')

In this example, we have initialized the array with five character values: A, B, C, D, and E.

To access an element in the array, you need to specify the index of the element within square brackets after the array name. For example, to access the second element in the array, you would use the following syntax:

scss
Copy code
CHGVAR VAR(&ELEMENT) VALUE(%SST(&MYARRAY 2 1))

In this example, we have assigned the value of the second element in the array to a variable named &ELEMENT using the %SST function.

  • Creating a Multi-Dimensional Array:

To create a multi-dimensional array, you need to use the DCL statement, followed by the VAR keyword, the array name, the TYPE keyword, the data type of the array elements, and the DIM keyword, the number of elements in each dimension. Here is an example:

scss
Copy code
DCL VAR(&MYARRAY) TYPE(*CHAR) DIM(3 4)

In this example, we have created a two-dimensional character array named &MYARRAY that can hold up to 3 rows and 4 columns of elements. To initialize the array with values, you can use the following syntax:

java
Copy code
DCL VAR(&MYARRAY) TYPE(*CHAR) DIM(3 4) VALUE('A1' 'B1' 'C1' 'D1' 'A2' 'B2' 'C2' 'D2' 'A3' 'B3' 'C3' 'D3')

In this example, we have initialized the array with twelve character values.

To access an element in a multi-dimensional array, you need to specify the row and column indexes of the element within square brackets after the array name. For example, to access the element in the second row and third column, you would use the following syntax:

scss
Copy code
CHGVAR VAR(&ELEMENT) VALUE(%SST(&MYARRAY 2 3))

In this example, we have assigned the value of the element in the second row and third column to a variable named &ELEMENT using the %SST function.

Overall, arrays in CL400 provide a way to store and manipulate collections of data elements. By understanding how to create and work with one-dimensional and multi-dimensional arrays, you can create more complex programs that can process and analyze large sets of data.


    Leave a Comment


  • captcha text