VBScript, similar to other programming languages, has the capabilities to use a data structure called an array. An array is a collection of data where each piece of data is held in a numbered position within the array. A one-dimensional array would be similar to a column in a spreadsheet.
Each position in the array is assigned an index number beginning with 0 for the first position, 1 for the second position, and so on. This allows any position within the array, or “element” of the array, to be referenced by its index number.The number contained within round brackets indicates the index of the array. An array can be created using the “dim” statement.
Dim a(2) : REM creates an array called “a”
a(0) = 1.2 : REM sets the first element
a(1) = "VBScript" : REM sets the second element
Arrays are important to understand because a number of components of the Document Object Model (DOM) are implemented as arrays, like forms, images,and elements.
Dim is short for dimension. When you declare a variable the interpreter needs to allocate memory to hold the contents for you. In strongly typed languages (which VBScript is not) the interpreter or compiler will know what the dimension (size of memory) is required to hold the value, based on the data type. In VBScript no memory is allocated until you store a value in the variable. When dimensioning an array it is important to tell the interpreter how many elements are going to be in the array. If you need to increase the number of elements you can redim(ension) the array. You have to be careful when redimensioning arrays because the values of the array will be dropped. To prevent the values from being dropped use the keyword “preserve” to keep the existing values. For example:
ReDim Preserve a(10)
The ReDim statement actually creates a new array and copies the values from the first smaller array into the bigger array. If the new array is smaller than the existing array some data will be lost.