ReverseArray
See Also: Array Functions, Array Variable Assignments, Working with Arrays
Purpose
Returns an array with elements in reverse order of the original array.
Return Type
Syntax
(ReverseArray({ArrayId}))
Where:
- {ArrayId} is the id of the array being reversed. The array must be non-jagged and one-dimensional (it can be a single dimension of a multi-dimensional array).
What it Does
ReverseArray reverses the order of the elements in an array.
Example 1
This sample reverses a dynamic array of integers (ints1) and places the result into the array ints2.
// fires when the button is clicked
Procedure OnClick
Integer[][] ints1 ints2
// add some code to populate ints1 array
// :
// reverse the array
Move (ReverseArray(ints1)) to ints2
End_Procedure
Example 2
This sample reverses a string array (sCustomers), then displays the reversed array elements to the screen.
// fires when the button is clicked
Procedure OnClick
String[] sCustomers
Integer i iArraySize
// populate array
Move "Smith" to sCustomers[0]
Move "Rodriguez" to sCustomers[1]
Move "Smith" to sCustomers[2]
Move "Jones" to sCustomers[3]
Move "Anderson" to sCustomers[4]
Move "Schmidt" to sCustomers[5]
Move "Verne" to sCustomers[6]
Move "Ricci" to sCustomers[7]
Move "Sorensen" to sCustomers[8]
Move "Garcia" to sCustomers[9]
// reverse the array
Move (ReverseArray(sCustomers)) to sCustomers
// display the elements in the reversed array
Move (SizeOfArray(sCustomers)) to iArraySize
For i From 0 to (iArraySize - 1)
Showln sCustomers[i]
Loop
End_Procedure
Example 3
Reversing a single dimension of a multi-dimensional array:
This sample declares a 2-dimensional string array and fills it with 8 (2x4) elements of unsorted string values. This creates an array as follows:
Smith Rodriguez Scott Jones
Anderson Schmidt Verne Ricci
The first "row" (row 0) of the array is then sorted and placed in row 0 of the resulting array (in this sample, the results of ReverseArray are placed back in the original array), resulting in this array:
Jones Scott Rodriguez Smith
Anderson Schmidt Verne Ricci
Procedure OnClick
String[][] sCustomers
Integer i j
// populate array
Move "Smith" to sCustomers[0][0]
Move "Rodriguez" to sCustomers[0][1]
Move "Scott" to sCustomers[0][2]
Move "Jones" to sCustomers[0][3]
Move "Anderson" to sCustomers[1][0]
Move "Schmidt" to sCustomers[1][1]
Move "Verne" to sCustomers[1][2]
Move "Ricci" to sCustomers[1][3]
// reverse the array's first "row"
Move (ReverseArray(sCustomers[0])) to sCustomers[0]
End_Procedure