VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, 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

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 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

 What Is an Array?

 "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

 "For Each" Statement 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

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"x(i)" - Accessing Array Elements with Indexes - Updated in 2015, by Dr. Herong Yang