advpl array

What is AdvPL Array?

AdvPL Array is a data type used in the AdvPL language for handling arrays. Arrays are a collection of data elements that are of the same data type, and each element can be accessed using an index number. AdvPL arrays can be one-dimensional, two-dimensional or multidimensional.

Creating an AdvPL Array

To create an AdvPL array, you need to declare it with a name and specify the number of elements in the array. Here's an example of how you can create a one-dimensional array with three elements:


user : array(3)

You can also initialize the values of the array elements when you declare it. Here's an example of how you can create a one-dimensional array with three elements and initialize the values:


user : array(3) {"John", "Mary", "Peter"}

Accessing AdvPL Array Elements

You can access individual elements of an AdvPL array using the index number. The index starts from 1 for the first element, 2 for the second and so on. Here's an example of how you can access the second element of a one-dimensional array:


user : array(3) {"John", "Mary", "Peter"}

// Accessing the second element
? user[2]
// Output: Mary

You can also use loops to access all elements of an AdvPL array. Here's an example of how you can use a for loop to iterate through all elements of a one-dimensional array:


user : array(3) {"John", "Mary", "Peter"}

for i := 1 to len(user)
    ? user[i]
next
// Output: John Mary Peter

AdvPL Multidimensional Array

You can create multidimensional arrays in AdvPL by specifying the number of elements in each dimension. Here's an example of how you can create a two-dimensional array with three rows and two columns:


matrix : array(3,2)

You can access the elements of a multidimensional array using the index numbers of each dimension. Here's an example of how you can access the element in the second row and first column of a two-dimensional array:


matrix : array(3,2)

// Assigning a value to the element in the second row and first column
matrix[2,1] := "X"

// Accessing the element in the second row and first column
? matrix[2,1]
// Output: X

You can use nested loops to iterate through all elements of a multidimensional array. Here's an example of how you can use nested loops to iterate through all elements of a two-dimensional array:


matrix : array(3,2)

// Initializing the values of the elements
for i := 1 to len(matrix)
    for j := 1 to len(matrix[1])
        matrix[i,j] := i + "-" + j
    next
next

// Accessing all elements
for i := 1 to len(matrix)
    for j := 1 to len(matrix[1])
        ? matrix[i,j]
    next
next
// Output: 1-1 1-2 2-1 2-2 3-1 3-2

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe