VBScript Array Concept
Table of content
What is an Array?
In order to hold the multiple values in same variable name we use array. In VBScript array have multiple values but all are having same variable name and those values are accessed using subscript variable.
Array Declaration :
We can declare an array in three ways -
- Without Specifing Size
- With Specifing Size
- Using Array Parameter
Arrays are declared the same way a variable has been declared except that the declaration of an array variable uses paranthesis. In the below example, the size of the array is mentioned in the brackets.
Dim arr1() 'Without Size Dim arr2(4) 'Declared with size of 5 Dim arr3 = Array("A","B","C") 'Using Array Parameter
Array Tips :
- In the second array declaration, we have specified 5 in the parenthesis, it can hold 6 values as array index starts from ZERO.
- Array Index should be positive, it cannot be Negative.
- We can store any type of variable in an array. We can store an integer, string or characters in a single array variable.
Example : VBScript Single Dimenstional Array
<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> Dim arr(5) arr(0) = "8" 'Number as String arr(1) = "VBScript" 'String arr(2) = 500 'Number arr(3) = 3.14 'Decimal Number arr(4) = #01/04/2014# 'Date arr(5) = #10.00 AM# 'Time document.write("Value at index 0 : " & arr(0) & "<br />") document.write("Value at index 1 : " & arr(1) & "<br />") document.write("Value at index 2 : " & arr(2) & "<br />") document.write("Value at index 3 : " & arr(3) & "<br />") document.write("Value at index 4 : " & arr(4) & "<br />") document.write("Value at index 5 : " & arr(5) & "<br />") </script> </body> </html>
Output :
Value at index 0 : 8 Value at index 1 : VBScript Value at index 2 : 500 Value at index 3 : 3.14 Value at index 4 : 1/4/2025 Value at index 5 : 10:00:00 AM
VBScript Multi Dimension Arrays :
Array having multiple dimensions are called as Multi-Dimensional Array. We can have a maxinum of 60 dimensions.
Example :
In the below example, a multi-dimension array is declared with 3 rows and 4 columns.
<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> Dim arr(2,2) ' Actual Array with 3X3 dimension arr(0,0) = "Cell 0-0" arr(0,1) = "Cell 0-1" arr(0,2) = "Cell 0-2" arr(1,0) = "Cell 0-0" arr(1,1) = "Cell 0-1" arr(1,2) = "Cell 0-2" arr(2,0) = "Cell 0-0" arr(2,1) = "Cell 0-1" arr(2,2) = "Cell 0-2" document.write("Value at 0,0 :" & arr(0,0) & "<br />") document.write("Value at 0,1 :" & arr(0,1) & "<br />") document.write("Value at 0,2 :" & arr(0,2) & "<br />") document.write("Value at 1,0 :" & arr(1,0) & "<br />") document.write("Value at 1,1 :" & arr(1,1) & "<br />") document.write("Value at 1,2 :" & arr(1,2) & "<br />") document.write("Value at 2,0 :" & arr(2,0) & "<br />") document.write("Value at 2,1 :" & arr(2,1) & "<br />") document.write("Value at 2,2 :" & arr(2,2) & "<br />") </script> </body> </html>
Output :
Value at index 0,0 : Cell 0-0 Value at index 0,1 : Cell 0-1 Value at index 0,2 : Cell 0-2 Value at index 1,0 : Cell 0-0 Value at index 1,1 : Cell 0-1 Value at index 1,2 : Cell 0-2 Value at index 2,0 : Cell 0-0 Value at index 2,1 : Cell 0-1 Value at index 2,2 : Cell 0-2
VBScript ReDim : Reallocate Storage Space
ReDim Statement is used to Declare dynamic-array variables and allocate or reallocate storage space.
ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Parameter | Explanation |
---|---|
Preserve | It is an optional parameter which is used to preserve the data in an existing array while changing the you change the size of the last dimension. |
VarName | It is required parameter used to denote the name of the array to be reallocated. |
Subscripts | It is required parameter which indicates the size of the array. |
Example : ReDim Array
Here we have redefined the array and preserved the values of the exisitng array.
<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> 'Create Array Dim a() ReDim a(3) a(0) = 1000 a(1) = 2000 a(2) = 3000 'Re allocate array ReDim PRESERVE a(5) a(3) = 4000 a(4) = 5000 a(5) = 6000 'Show Output For i=0 to ubound(a) document.write(a(i) & " ") Next </script> </body> </html>
Output :
1000 2000 3000 4000 5000 6000