VBScript Tutorials - Herong's Tutorial Examples - Version 5.23, by Dr. Herong Yang
"x(i)" - Accessing Array Elements with Indexes
This section describes how to access array elements for assigning new values and retrieving existing values. 'For Each' statement can also be used to loop through all elements in an array.
If you want to access a specific element of an array, you need to use the array element syntax, which is the variable name followed by parentheses containing an index number indicating the desired element.
New values can be assigned to individual elements in an array using assignment statements like this:
array_variable(index) = new_value
where "index" is the index value of an element of the array associated with "array_variable", and "new_value" is the new value to be assigned the array element.
Retrieving values from array elements can be done through their indexes like this:
... array_variable(index) ...
VBScript also offers a special loop statement, "For Each" statement, to retrieve every element in an array like this:
For Each element_variable In array_variable ... element_variable ... Next
This statement will iterate through every element in the specified array. At each iteration, the value of the current element will be copied to the specified temporary variable, "element_variable". The order of iteration is based on element index values from the lower bound to the upper bound.
Here are example statements of assigning values to array elements and retrieving them back.
Dim list(2) list(0) = "Apple" list(1) = "Orange" list(2) = "Peach" document.writeln(list(0)) document.writeln(list(1)) document.writeln(list(2))
Watch out - Array element syntax in VBScript is different than other languages. For example, Java uses "a[i]" instead of "a(i)" to represent an array element.
Table of Contents