Basic part of VBScript – definition and use of VBS array

Time:2021-11-25

vbsarrayDefinition and Usage

ArrayReturns a variable that contains an array.
Note: the first element in the array is zero.

grammar

Array(arglist)
parameter   describe
arglist   Required. A comma separated list of element values in the array.

example

Example 1

?
1
2
3
dim a
a=Array(5,10,15,20)
document.write(a(3))

Output:

20

Example 2

?
1
2
3
dim a
a=Array(5,10,15,20)
document.write(a(0))

Output:
5

Array variable: sometimes you need to assign multiple values to a single variable, so you can create a variable that can contain a series of values, which is called an array.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'static array
dim a(2)
a(0)="George"
a(1)="john"
a(2)="Ethon"
for i =0 to 2
  msgbox a(i)
next
for i =0 to ubound(a) 'UBound function returns the maximum available subscript of the specified array dimension
  msgbox a(i)
next
 
 
'dynamic array
dim a()
for i = 0 to 2
  redim preserve a(i) 'redim statement, which is used to declare dynamic array variables and allocate or reallocate storage space at the procedure level
  a(i)=i+1
  msgbox a(i)
next

Dynamic two-dimensional array

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
'dynamic 2D array
Dim MyArray() 'first define a one-dimensional dynamic array
ReDim MyArray(1,1) 'redefine the size of the array
MyArray(0,0) = "A" 'assign values to the array respectively
MyArray(0,1) = "a"
MyArray(1,0) = "B"
MyArray(1,1) = "b"
ReDim Preserve MyArray(1,2) 'redefine the size of the array
MyArray(0,2) = "A-a" 'continue assigning values to the array
MyArray(1,2) = "B-b"
MsgBox UBound(MyArray,1)
MsgBox UBound(MyArray,2)
For i=0 To UBound(MyArray,1)
  For j=0 To UBound(MyArray,2)
    MsgBox MyArray(i,j) 'loop through the array and output the array value
  Next
Next

One dimensional dynamic array

?
1
2
3
4
5
6
7
8
9
10
11
12
Dim MyArray() 'first define a one-dimensional dynamic array
ReDim MyArray(3) 'redefine the size of the array
MyArray(0) = "I" 'assign values to the array respectively
MyArray(1) = "Yes"
MyArray(2) = "Learning"
MyArray(3) = "Xi"
ReDim Preserve MyArray(5) 'redefine the size of the array
MyArray(4) = "Test" 'continue assigning values to the array
MyArray(5) = "Try"
For i=0 To UBound(MyArray)
  MsgBox MyArray(i) 'loop through the array and output the array value
Next

This article is introduced here. Friends in need can refer to it.

Recommended Today

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]