udfArrayReverse
arr udfArrayReverseV1 (arr)
arr udfArrayReverseV2 (arr)
arr udfArrayReverseV3 (arr)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayReverseV1 (arrArray)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.AllowUI = @FALSE
Switch @TRUE
Case VarType (arrArray) == 256 ; WinBatch Array.
Case ObjectTypeGet (arrArray) == "ARRAY|VARIANT" ; Variant Array.
   objSC.Language = "jscript"
   objSC.AddCode(: "function VB2JSArray(objVBArray){var a;try{a=new VBArray(objVBArray).toArray();}catch(e){a=new Array(objVBArray);}return a;}")
   objSC.AddCode(: 'function JS2VBArray(objJSArray){var dict=new ActiveXObject("Scripting.Dictionary");for(var i=0;i<objJSArray.length;i++){dict.add(i,objJSArray[i]);}return dict.Items();}')
   objSC.AddCode(: "function ArrayReverse(objVBArray){return JS2VBArray(VB2JSArray(objVBArray).reverse());}")
   arrArray = objSC.Run(: "ArrayReverse", arrArray)
   Break
Case @TRUE
   objSC.Language = "vbscript"
   objSC.AddCode(: "Dim a()")
   arrArray = objSC.Eval(: "a")
EndSwitch
Return arrArray
;..........................................................................................................................................
; This UDF "udfArrayReverseV1" returns a zero-based Dim-1 variant array that contains all elements from the given array in reversed order
; (makes the last element first, and the first element last).
;
; Parameter:
; arrArray ... A one-dimensional array. Array type can be WinBatch Array or Variant Array.
;..........................................................................................................................................
; Alternative codeline, see above:
; objJSC.AddCode(: "function VB2JSArray(objVBArray){return new VBArray(objVBArray).toArray();}") ; Possibly in some circumstances not so safe to use.
;..........................................................................................................................................
; (c)Detlev Dalitz.20100121.
; Jim Dippner.20080221: changed VB2JSArray.
; David Wang.20060704: created VB2JSArray, JS2VBArray.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayReverseV2 (arrArray) ; WinBatch array.
If ArrInfo (arrArray, 0) != 1 Then Return arrArray ; Only Dim-1 array allowed.
intLast = ArrInfo (arrArray, 1)
If intLast == 0 Then Return ArrDimension (0)
arrRev = ArrDimension (intLast)
intLast = intLast - 1
For intElem = 0 To intLast
   arrRev[intLast - intElem] = arrArray[intElem]
Next
Return arrRev
;..........................................................................................................................................
; This UDF "udfArrayReverseV2" returns a zero-based Dim-1 WinBatch array that contains all elements from the given Dim-1 WinBatch array in reversed order
; (c)Detlev Dalitz.20100121.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayReverseV3 (arrArray) ; WinBatch array.
If ArrInfo (arrArray, 0) != 1 Then Return arrArray ; Only Dim-1 array allowed.
intLast = ArrInfo (arrArray, 1)
If intLast == 0 Then Return ArrDimension (0)
intMid = intLast / 2
For intLeft = 1 To intMid
   intRight = intLast - intLeft
   anyTemp = arrArray[intLeft - 1]
   arrArray[intLeft - 1] = arrArray[intRight]
   arrArray[intRight] = anyTemp
Next
Return arrArray
;..........................................................................................................................................
; This UDF "udfArrayReverseV3" returns a zero-based Dim-1 WinBatch array that contains all elements from the given Dim-1 WinBatch array in reversed order
; (c)Detlev Dalitz.20100121.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDisplayArrayElements (arrArray) ; WinBatch or Variant Dim-1 array only.
intElements = ArrInfo (arrArray, 1)
If intElements > 0
   intElemLast = intElements - 1
   For intElem = 0 To intElemLast
      Message ("Array|Elements=" : intElements : "|Index=" : intElem, "Content=" : arrArray[intElem])
   Next
Else
   Message ("Array|Elements=" : intElements, "No elements ... nothing to do.")
EndIf
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; Create a WB Dim-1 array.
Message ("WB example for ArrayReverse.", "Next step:" : @LF : "Create WB array and display all elements in WB array.")
arrWB = ArrDimension (7)
arrWB[0] = "Monday"
arrWB[1] = "Tuesday"
arrWB[2] = "Wednesday"
arrWB[3] = "Thursday"
arrWB[4] = "Friday"
arrWB[5] = "Saturday"
arrWB[6] = "Sunday"
udfDisplayArrayElements (arrWB) ; Result: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.


; Example 1.
Message ("Ex. 1", "Next example:" : @LF : "Reverse all elements in array and return new array with the result.")
arrReversed = udfArrayReverseV1 (arrWB)
udfDisplayArrayElements (arrReversed) ; Result: Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, Monday.


; Example 2.
Message ("Ex. 2", "Next example:" : @LF : "Reverse all elements in WinBatch array and return new array with the result.")
arrReversed = udfArrayReverseV2 (arrWB)
udfDisplayArrayElements (arrReversed) ; Result: Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, Monday.


; Example 3.
Message ("Ex. 3", "Next example:" : @LF : "Reverse all elements in WinBatch array and return new array with the result.")
arrReversed = udfArrayReverseV3 (arrWB)
udfDisplayArrayElements (arrReversed) ; Result: Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, Monday.

Exit