Array declaration
Dim var (ten)
the array declaration indicates the maximum value of the array name and index (index). The array index starts at 0. So the statement creates an array of 11 elements, where the value is var (0), VAR (1), ... and VAR (10)
Iterate through all elements of an array
You can use the For Each loop without specifying the number of elements in the array beforehand. This method does not apply if you do not want to process each element of the array.
You can also iterate through the array by indexing each value sequentially.
The following shows each of these two ways.
Dimvar5) Var (0) =0var (1) =1var (2) =2'fewer items are used than the number of elements in the arrayNitems =3 for eachValueinchvar wscript.echo valueNext fori =0 toNitems-1wscript.echo var (i)Next
The output is as follows
0
1
2
0
1
2
Since the array actually has 6 elements, but only three of them are initialized, and when the array is traversed in the first way, the uninitialized array elements are traversed, and the output has three lines of whitespace.
Variable-size arrays
If necessary, set the array so that it is variable in size. First, you do not have to specify the size when declaring an array
Dim VarName ()
You can use the ReDim statement to set or reset the size of an array before the first use, and at any time thereafter
ReDim [Preserve] varname (subscripts) [, ...]
The keyword Preserve makes VBScript keep existing data in a resized array. (If the new array is shorter than the original, the extra elements at the end are removed). If there is no preserve, the declared variable and all elements are set to nothing
VBScript Learning notes-arrays