VBScript Tutorials - Herong's Tutorial Examples - v6.02, by 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
Introduction of VBScript - Visual Basic Scripting Edition
Variant Data Type, Subtypes, and Literals
Numeric Comparison Operations and Logical Operations
String Operations - Concatenation and Comparison
Variable Declaration and Assignment Statement
Expression and Order of Operation Precedence
Statement Syntax and Statement Types
►Array Data Type and Related Statements
"Dim x()" - Declaring Array Variables
►"x(i)" - Accessing Array Elements with Indexes
"Dim x(n)" - Fixed-Size Array Example
"Dim x()" - Dynamic-Size Array Example
"Erase" Statement - Removing All Elements in an Array
Data Type "Variant()" - Array of Variant Values
Array References and Array Assignment Statements
Conditional Statements - "If ... Then" and "Select Case"
Loop Statements - "For", "While", and "Do"
"Function" and "Sub" Procedures
Inspecting Variables Received in Procedures
Error Handling Flag and the "Err" Object
Regular Expression Pattern Match and Replacement
scrrun.dll - Scripting Runtime DLL Library
IE Web Browser Supporting VBScript