CopyArray
See Also: Array Functions, Array Variable Assignments, Working with Arrays
Purpose
Returns an array representing a range of elements from the existing array.
Return Type
Syntax
(CopyArray({ArrayId}, {indexFirst}, {indexLast}))
Where:
- {ArrayId} is the id of the array to retrieve a range of elements from.
- {indexFirst} is the index of the first element in the range to retrieve.
- {indexLast} is the index of the last element in the range to retrieve.
What it Does
Creates a new array from elements within the range of the existing array.
Example 1
This sample declares an integer array (integers1) and fills it with the numbers 1 through 100. Next, it copies the first 20 elements of integers1 to the array (integers2), then displays all elements of the new array (integers2).
// fires when the button is clicked
Procedure OnClick
Integer[] integers1 integers2
Integer i iSize
For i From 0 to 99
Move (i + 1) to integers1[i]
Loop
Move (CopyArray(integers1, 0, 19)) to integers2
Move (SizeOfArray(integers2)) to iSize
For i From 0 to (iSize - 1)
showln "integers2[" (string(i)) "] = " (string(integers2[i]))
Loop
End_Procedure
Example 2
This sample declares a two-dimensional dynamic array (integers1), containing an undefined number of elements of type integer. It then fills the second dimension of the array 10 times with the numbers 1 through 10, creating a total of 100 elements. Next, the sample copies the first 5 outer elements of integers1 to a dynamic array (integers2), then displays all elements of the new array (integers2).
// fires when the button is clicked
Procedure OnClick
Integer[][] integers1 integers2
Integer i j iSize
For i From 0 to 9
For j From 0 to 9
Move (j + 1) to integers1[i][j]
Loop
Loop
Move (CopyArray(integers1, 0, 5)) to integers2
Move (SizeOfArray(integers2)) to iSize
For i From 0 to (iSize - 1)
For j From 0 to 9
showln "integers2[" (string(i)) "][" (string(j)) "] = " (string(integers2[i][j]))
Loop
Loop
End_Procedure
Notes
- To make a full copy of an array, simply move an existing array to another array variable. This copies the array using a deep memberwise copy operation.
Example 3
This sample declares an integer array (integers1) and fills it with the numbers 1 through 100. Next, it copies the entire array integers1 (including the contents of all elements) to the array integers2, then displays all elements of the new array (integers2).
// fires when the button is clicked
Procedure OnClick
Integer[] integers1 integers2
Integer i iSize
For i From 0 to 99
Move (i + 1) to integers1[i]
Loop
// full array copy
Move integers1 to integers2
Move (SizeOfArray(integers2)) to iSize
For i From 0 to (iSize - 1)
showln "integers2[" (string(i)) "] = " (string(integers2[i]))
Loop
End_Procedure