VBS array in simple terms

Time:2021-11-24

VBS tutorial using arrays

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
REM definition
dim arr(5)
 
REM assignment
arr(0) = 20
arr(1) = 2
arr(2) = 5
arr(3) = 3
arr(4) = 1
arr(5) = 100
 
REM access
msgbox(arr(2))
 
REM traversal
for each v in arr
  msgbox(v)
next

VBS array is not as widely used as other statements. VBS array has many functional limitations (such as the definition and assignment of two-dimensional array), and it is not as convenient as Java and other languages. Let’s explain how to use the following array functions:

Array function

Returns the variant containing the array.

Array(arglist)

The arglist parameter is a comma separated list of values assigned to the array elements contained in the variant. If this parameter is not specified, a zero length array is created.

explain

An indicator used to refer to array elements, consisting of variable names followed by parentheses containing the index number indicating the desired element. In the following example, the first statement creates a variable named a. The second statement assigns an array to variable a. The last statement assigns the value contained in the second array element to another variable.

?
1
2
3
arr = array("t1","t2")
MsgBox arr(0)
MsgBox arr(1)

 be carefulVariables that are not declared as arrays can still contain arrays. Although the variant variable containing the array is conceptually different from the array variable containing the variant element, the method of accessing the array element is the same.

Isarray function

Returns a Boolean value indicating whether a variable is an array.

IsArray(varname)
The Varname parameter can be any variable.

explain

If the variable is an array, the isarray function returns true; Otherwise, the function returns false. It is effective to use the isarray function when the variable contains an array.

The following example uses the isarray function to verify whether myvariable is an array:

?
1
2
3
4
5
6
Dim MyVariable
Dim MyArray(3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyVariable = IsArray(MyArray) 'myvariable contains' true '.

UBound function
Returns the maximum available subscript for the specified array dimension.

UBound(arrayname[, dimension])
parameter

arrayname

Required. Array variable name, following the standard variable naming rules.

Dimension

Optional. An integer that specifies the upper bound of which dimension to return. 1 represents the first dimension, 2 represents the second dimension, and so on. If the dimension parameter is omitted, the default value is 1.

explain

The UBound function is used with the lbound function to determine the size of the array. The lbround function can be used to determine the lower bound of a certain dimension of the array.

The lower bound of all dimensions is 0. For arrays with such dimensions, the UBound function returns the following results:

Dim A(100,3,4)

 

sentence

Return value

UBound(A, 1)

100

UBound(A, 2)

3

UBound(A, 3)

4

 

Lbound function
Returns the minimum available subscript for the specified array dimension.

LBound(arrayname[, dimension])
parameter

arrayname

Array variable name, following the standard variable naming rules.

Dimension

An integer indicating which one-dimensional lower bound to return. Use 1 for the first dimension, 2 for the second dimension, and so on. If the dimension parameter is omitted, the default value is 1.

explain

The lbound function is used in conjunction with the UBound function to determine the size of the array. Using the UBound function, you can find the upper bound of a certain dimension of the array. The lower bound of any dimension is 0.

?
1
2
3
4
arr = array("t1","t2","t3")
For i=0 To UBound(arr)-LBound(arr)
 MsgBox arr(i)
Next

Split function
Returns a 0-based one-dimensional array containing a specified number of substrings.

Split(expression[, delimiter[, count[, start]]])
parameter

expression

Required. A string expression that contains substrings and delimiters. If expression is a zero length string, split returns an empty array, that is, an array without elements and data.

delimiter

Optional. A character that identifies the bounds of a substring. If omitted, use a space (“”) as the separator. If the delimiter is a zero length string, a single element array containing the entire expression string is returned.

count

Optional. The number of substrings returned, – 1 indicates that all substrings are returned.

Compare

Optional. A numeric value indicating the type of comparison to use when evaluating substrings. For values, see the settings section.

set up

The compare parameter can have the following values:

 

constant

value

describe

vbBinaryCompare

0

Perform a binary comparison.

vbTextCompare

1

Perform a text comparison.

 

explain

The following example uses the split function to return an array from a string. The function compares the delimiter text and returns all substrings.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
'myarray (0) contains' VBScript '.
'myarray (1) contains' is'.
'myarray (2) contains' fun! '.
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
 
Another example:
stng = "t1,t2,t3"
arr = Split(stng,",")
MsgBox arr(0)
MsgBox arr(1)
MsgBox arr(2)

Join function
Returns a string created by concatenation of many substrings contained in an array.

Join(list[,delimiter])
parameter

list

Required. A one-dimensional array containing substrings to join.

Delimiter

Optional. The character used to separate substrings in the return string. If omitted, the null character (“”) will be used. If the delimiter is a zero length string, all items are listed in the same list without delimiters.

The following example uses the join function to combine substrings of myArray:

?
1
2
3
4
5
6
7
8
9
10
11
Dim MyString
Dim MyArray(3)
MyArray(0) = "Mr."
MyArray(1) = "John "
MyArray(2) = "Doe "
MyArray(3) = "III"
MyString = Join(MyArray) 'mystring contains' Mr. John Doe III '.
 
Another example:
arr = array("t1","t2","t3")
MsgBox Join(arr,",")

The above mainly introduces the related functions in VBS, and some special treatments are often encountered in practical work, which are very practical. The following examples are given one by one:

Example 1: comparison of one-dimensional arrays

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dim a
Dim b
a=Array(10,15,30)
b=Array(10,20,30)     
flag=1
For i=0 To UBound(a)-LBound(a)
  If a(i)=b(i) Then
   flag=1
  Else
   flag=0
   n=UBound(a)
   msgbox "Array a"&n&":="&a(i)&","&"Array B"&n&":="&b(i)
  End If
Next

Example 2: comparison of two-dimensional arrays

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Dim array1(1,1)
array1(0,0)=1
array1(0,1)=2
array1(1,0)=3
array1(1,1)=4
Dim array2(1,1)
array2(0,0)=1
array2(0,1)=2
array2(1,0)=4
array2(1,1)=4
     
flag=1
For i=0 To 1
 For j=0 To 1
  If array1(i,j)=array2(i,j) Then
   flag=1
   Exit for
  Else
   flag=0
   msgbox "array1"&"("&i&","&j&")"&"="&array1(i,j)&","&"array2"&"("&i&","&j&")"&"="&array2(i,j)
  End If
 Next
Next

Example 3: use a loop to compare whether the array contains a value

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Dim arr, i, str
arr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "f", "g")
str = "a"
For i = 0 To UBound(arr)
 If arr(i) = str Then
  Exit For
 end if
Next
If i <= UBound(arr) Then
 msgbox "Arr contains STR value!"
 Else
 msgbox "The value of STR is not contained in arr!"
End If

Example 4: use function combination to compare whether the array contains a value (optimization logic)

?
1
2
3
4
5
6
7
8
Dim arr, i, str
arr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "f", "g")
str = "a"
If InStr(Join(arr, "|"), str) > 0 Then
 msgbox "Arr contains STR value!"
 Else
 msgbox "The value of STR is not contained in arr!"
End If

Flexible application of various function commands can save a lot of redundant code, simplify the code structure and provide execution efficiency.

Example 5: the array in VBS is used as the return value of the function

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Function GenerateRandom()
 Dim myarray(2)
 Dim aa, bb, cc
 Dim myvalue, bbbase, ccbase
 bbbase=array("Beijing", "NewYork", "Copenhagen", "Paris", "London", "Gothenborg")
 ccbase=array("China", "America", "Denmark", "Franch", "England", "Sweden")
 'Get a number contains 8 characters
 aa= Int((99999999 - 11111111+ 1) * Rnd + 11111111)
 ' Get a number between 1 to 6
 myvalue=Int((6 * Rnd) + 1)
 bb=bbbase(myvalue)
 cc=ccbase(myvalue)
 myarray(0)=CStr(aa)
 myarray(1)=bb
 myarray(2)=cc
 GenerateRandom=myarray
 End Function
'****************************************************
' Call the function
Dim testarr
testarr=GenerateRandom
msgbox testarr(0)
msgbox testarr(1)
msgbox testarr(2)

Example 6: array sorting

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function fSortArray(aSortThisArray)
 Dim oArrayList, iElement
 Set oArrayList = CreateObject( "System.Collections.ArrayList" )
 For iElement = 0 To UBound(aSortThisArray)
  oArrayList.Add aSortThisArray(iElement)
 Next
 oArrayList.Sort
 set fSortArray = oArrayList
End Function
 
myarray=Array(50,20,30)
MsgBox myarray(0)
MsgBox fSortArray(myarray)(0)
 
'CreateObject ('system. Collections. ArrayList') calls mscoree.dll, which is a related component of the. Net framework.

Focus on automation and performance research. The blog is original. Please indicate the source of the article

Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]