|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
SArr_Reverse() - Reverse the order of elements in a string arrayThis function is specifically designed to be a METHOD in an object. Objects cannot be passed string arrays because PowerBasic objects are COM objects, and COM objects can't accept string arrays as parameters. The way to work around this is to use Variants. In the following function, a variant is used to receive the incoming string array, and a variant is used to return the string array.
METHOD SArr_Reverse(BYVAL MyArr AS VARIANT) AS VARIANT
'Purpose
' Used to reverse the order of elements in a string array.
'Parameters
' MyArr (Variant String Array) = The array we're working with.
'Return (Variant String Array)
' Returns the specified string array (MyArr) with all elements reversed.
'Note
' This is only designed to work with zero-based arrays.
' If the LBOUND() of the array is not zero, this function will fail.
'Version History
' 1.0 2010.09.11 Tested by Russ Tanner on PB/CC 5.05.
'---
DIM i AS LONG 'Iterator.
DIM a() AS STRING : REDIM a(0) 'Source buffer.
DIM b() AS STRING 'Destination buffer.
'Convert the variant string array into a native string array.
a() = MyArr
'Create a buffer array with enough elements to hold the original array.
REDIM b(UBOUND(a))
'Iterate the original array and transfer elements into the buffer array.
FOR i = 0 TO UBOUND(a)
b(UBOUND(a)-i) = a(i)
NEXT i
'Now, simply return the new array, which has the elements reversed.
METHOD = b()
END METHOD
|
|
|
|
Entire Thread
|
SArr_Reverse() - Reverse the order of elements in a string array
|
Russ
|
09/11/10 10:08 AM
|
|
|
|