VBScript array

From StealthBot Wiki Backup
Revision as of 16:01, 9 November 2011 by Ribose (talk) (Clearing an array)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

An array is a special variable containing a set of variables, where each index is like a variable. In VBScript the contents of each "box" can be similar (for example, all Integers) or all completely different (for example, one String followed by an Object).

Declaring

In VBScript, arrays go from 0 (lower bound) to an upper bound. You pass in an upper bound in the declaration, and there will be that many "boxes" in the the array plus the 0 "box".

To declare a static array, use the Dim statement but pass a number in parentheses:

Dim Arr(9)

There will be 10 "boxes" with the indexes shown (visualization, all are Empty):

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|   |   |   |   |   |   |   |   |   |   |

Assign a value in the same way, any way you like:

Arr(0) = "hi"
Arr(1) = 1
Arr(3) = 2
Arr(2) = 3
Arr(4) = 4
Arr(5) = 5
Arr(6) = 6
Arr(7) = 7
Arr(8) = 9
Arr(9) = 12

And the array will store it (visualization):

| 0  | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  |
|"hi"| 1 | 3 | 2 | 4 | 5 | 6 | 7 | 9 | 12 |

Accessing

Access it the same way as a variable as before, but specifying the index (the below will print "hi"):

AddQ Arr(0)

Loops

The For loop is well designed for looping through arrays with known or unknown length.

Loop through all elements in an array and display them (using the built-in UBound() function):

For I = 0 To UBound(Arr)
    AddQ Arr(I)
Next

Dynamic arrays and ReDim

You can also make an array that has a changing amount of elements, depending on what you need.

Declaring dynamic arrays

To declare a dynamic array, do not put the upper bound in the Dim statement, or the array will be static.

Dim DynArr

To make it an array, you have two simple ways:

  1. Use the Array() built-in function and store the result in the variable, which will pass an array with the provided starting contents.
    DynArr = Array("a", "b") ' < there are now two "boxes", "0" contains "a" and "1" contains "b".
  2. Use the ReDim statement, providing its upper bound.
    ReDim DynArr(1) ' < there are now two "boxes", one called "0" and one called "1", both empty
    ' then you can populate the initial contents if you want
    DynArr(0) = "a"
    DynArr(1) = "b"
    

Other ways to create a dynamic array are to use other built-in functions that return arrays, such as Split().

Visualization:

|  0  |  1  |
| "a" | "b" |

You can dynamically add or remove elements of the array later using the ReDim statement and the result of UBound().

To add a new array element:

ReDim DynArr(UBound(DynArr) + 1)

Visualization:

| 0 | 1 | 2 |
|   |   |   |

To remove the last array element:

ReDim DynArr(UBound(DynArr) - 1)

Visualization:

| 0 | 1 |
|   |   |

Note: The ReDim statement can take variables and functions and operators for its upper bound value, but Dim can only take a literal Integer.

Important: Executing either of the above two ReDim statements will clear the entire contents of the array. To preserve elements use the Preserve keyword.

Preserving array elements

The Preserve keyword tells ReDim we want to keep the previous elements. So lets add and remove from this state (visualization):

|  0  |  1  |
| "a" | "b" |

To add a new array element:

ReDim Preserve DynArr(UBound(DynArr) + 1)

Visualization:

|  0  |  1  | 2 |
| "a" | "b" |   |

To remove the last two array elements:

ReDim Preserve DynArr(UBound(DynArr) - 2)

Visualization:

|  0  |
| "a" |

This method is slow! The statement is creating a new array in memory and copying every element over when you do ReDim Preserve. If you need to add and remove elements a lot, a Scripting.Dictionary object may be better suited.

Clearing an array

The Erase statement allows you to clear the contents of an array variable. The contents of the variable will then be a completely empty variable of type array. This is like undoing the first ReDim statement. You will now be required to use ReDim before populating, accessing, or doing anything else on this array variable.

Dim Arr()
' Arr is of type Variant(), but cannot be used
ReDim Arr(UB)
' Arr now has the value of UB elements in it
' do stuff with Arr
' now we're done
Erase Arr
' Arr is again in the state it was right after the Dim statement

Doing this to statically declared arrays will only empty the contents. The number of elements will not be changed.

Dim Arr(9)
' Arr has 9 elements
' do stuff with Arr
' now we're done
Erase Arr
' Arr still has 9 elements, all set to Empty
' Arr is again in the state it was right after the Dim statement

See also