Developer: RhinoScript
Summary: Presents an array of VBScript Array utilities.
Arrays are a very useful and easy way of storing variables - and they're especially easy to use in VBScript. This is due to several factors:
But, VBScript falls short, compared to other programming languages, when it comes to providing tools for manipulating arrays. Some common array operations that VBScript does not provide methods for are:
For some of these tasks, RhinoScript does provide useful methods.
For the other tasks, you will need to write your own procedures. Fortunately, VBScript provides us enough tools to write our own procedures to do most of what we would ever want to do with an array. Note, the following examples are general purpose utilities. If you need something more specific, these examples are a good starting point.
Sub ArrayAdd(ByRef arr, ByVal val) Dim ub If IsArray(arr) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + 1) arr(UBound(arr)) = val End If End Sub
Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, ub If IsArray(arr) And IsArray(arr0) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + UBound(arr0) + 1) For i = 0 To UBound(arr0) arr(ub + 1 + i) = arr0(i) Next End If End Sub
This example uses the .NET ArrayList object:
Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next For i = 0 To UBound(arr0) Call list.Add(arr0(i)) Next arr = list.ToArray() End If End Sub
Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i If IsArray(arr) Then If pos > UBound(arr) Then Call ArrayAdd(arr, val) ElseIf pos >= 0 Then ReDim Preserve arr(UBound(arr) + 1) For i = UBound(arr) To pos + 1 Step -1 arr(i) = arr(i - 1) Next arr(pos) = val End If End If End Sub
This example uses the .NET ArrayList object:
Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Insert(pos, val) arr = list.ToArray() End If End Sub
Sub ArrayRemove(ByRef arr) If IsArray(arr) Then If UBound(arr) > -1 Then ReDim Preserve arr(UBound(arr) - 1) End If End If End Sub
Sub ArrayRemoveAt(ByRef arr, ByVal pos) Dim i If IsArray(arr) Then If pos >= 0 And pos <= UBound(arr) Then For i = pos To UBound(arr) - 1 arr(i) = arr(i + 1) Next ReDim Preserve arr(UBound(arr) - 1) End If End If End Sub
This example uses the .NET ArrayList object:
Sub ArrayRemoveAt(ByRef arr, ByVal pos) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.RemoveAt(pos) arr = list.ToArray() End If End Sub
Sub ArrayRemoveVal(ByRef arr, ByVal val) Dim i, j If IsArray(arr) Then i = 0 : j = -1 For i = 0 To UBound(arr) If arr(i) <> val Then j = j + 1 arr(j) = arr(i) End If Next ReDim Preserve arr(j) End If End Sub
This example uses VBScript's Dictionary object:
Sub ArrayCull(ByRef arr) Dim i, dict If IsArray(arr) Then Set dict = CreateObject("Scripting.Dictionary") For i = 0 To UBound(arr) If Not dict.Exists(arr(i)) Then Call dict.Add(arr(i), arr(i)) End If Next arr = dict.Items End If End Sub
This example uses the .NET ArrayList object:
Sub ArrayCull(ByRef arr) Dim i, list, tmp If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) If Not list.Contains(arr(i)) Then Call list.Add(arr(i)) End If Next arr = list.ToArray() End If End Sub
Function ArraySearch(ByRef arr, ByVal val) Dim i ArraySearch = -1 If IsArray(arr) Then For i = 0 To UBound(arr) If arr(i) = val Then ArraySearch = i Exit Function End If Next End If End Function
This example uses a simple Bubble Sort algorithm:
Sub ArraySort(ByRef arr) Dim i, j, tmp If IsArray(arr) Then For i = UBound(arrShort) - 1 To 0 Step -1 For j = 0 To i If arr(j) > arr(j + 1) Then tmp = arr(j + 1) arr(j + 1) = arr(j) arr(j) = tmp End If Next Next End If End Sub
This example uses the .NET ArrayList object:
Sub ArraySort(ByRef arr) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Sort() arr = list.ToArray() End If End Sub
Sub ArrayReverse(ByRef arr) Dim i, ub, ub2, tmp ub = UBound(arr) ub2 = Int(ub / 2) For i = 0 To ub2 tmp = arr(i) arr(i) = arr(ub - i) arr(ub - i) = tmp Next End Sub
This example uses the .NET ArrayList object:
Sub ArrayReverse(ByRef arr) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Reverse() arr = list.ToArray() End If End Sub
Function IsArrayDim(ByVal arr) IsArrayDim = False If IsArray(arr) Then On Error Resume Next Call UBound(arr) If Err.Number = 0 Then IsArrayDim = True End If End Function
Public Function GetArrayDim(ByVal arr) Dim i If IsArray(arr) Then For i = 1 To 60 On Error Resume Next Call UBound(arr, i) If Err.Number <> 0 Then GetArrayDim = i - 1 Exit Function End If Next GetArrayDim = i Else GetArrayDim = Null End If End Function
Sub ArrayPrint(ByVal arr) Dim i If IsArray(arr) Then For i = 0 To UBound(arr) Call Rhino.Print("Item(" & CStr(i) & ") = " & CStr(arr(i))) Next End If End Sub