Analysis of _vbs of VBS array Function Learning Example

Source: Internet
Author: User
Tags arrays numeric join naming convention numeric value
Here are some examples of how the following array functions are used:

Array function

Returns a Variantthat contains an array.

Array (arglist)

The arglist parameter is a comma-delimited list of values assigned to the array elements contained in the Variant . If this parameter is not specified, an array of 0 lengths is created.

Description

A representation that references an array element, consisting of a variable name followed by parentheses that contains 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.

arr = Array ("T1", "T2")
MsgBox arr (0)
MsgBox arr (1)

Note A variable that is not declared as an array can still contain an array. Although the variant variable that contains the array is conceptually different from the array variable that contains the variant element, the method to access the array elements is the same.

IsArray function

Returns a Boolean value that indicates whether a variable is an array.

IsArray (varname)

The varname parameter can be any variable.

Description

If the variable is an array, theIsArray function returns True, otherwise the function returns False. Using the IsArray function works well when a variable contains an array.

The following example uses the IsArray function to verify that the myvariable is an array:

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 number of available subscripts for the specified array dimension.

UBound (arrayname[, Dimension])

Parameters

Arrayname

Required option. An array variable name followed by a standard variable naming convention.

Dimension

Options available. An integer that specifies which dimension upper bound is returned. 1 represents the first dimension, 2 represents the second dimension, and so on. If the dimension argument is omitted, the default value is 1.

Description

The UBound function is used in conjunction with the LBound function to determine the size of the array. You can use the LBound function to determine the lower bound of one dimension of an array.

The lower bounds of all dimensions are 0. For arrays with such dimensions, theUBound function returns the following result:

Dim A (100,3,4)

Statement return value
UBound (A, 1) 100
UBound (A, 2) 3
UBound (A, 3) 4

LBound function

Returns the lowest available subscript for the specified array dimension.

LBound (arrayname[, Dimension])

Parameters

Arrayname

An array variable name followed by a standard variable naming convention.

Dimension

An integer that indicates which dimension lower bound to return. Using 1 means the first dimension, 2 represents the second dimension, and so on. If the dimension argument is omitted, the default value is 1.

Description

The LBound function is used in conjunction with the UBound function to determine the size of the array. You can use the UBound function to find an upper bound of one dimension of an array. The lower bound of the Zing Ivi is 0.

arr = Array ("T1", "T2", "T3") for
i=0 to UBound (arr)-lbound (arr) MsgBox arr
 (i)
Next

Split function

Returns a one-dimensional array based on 0 that contains the specified number of substrings.

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

Parameters

Expression

Required option. A string expression that contains substrings and delimiters. If expression is a zero-length string,Split returns an empty array, which is an array that contains no elements and data.

Delimiter

Options available. The character used to identify the bounds of the substring. If omitted, use a space ("") as the separator. If delimiter is a zero-length string, returns the set of cell numbers that contain the entire expression string.

Count

Options available. The number of substrings returned,-1 indicates that all substrings are returned.

Compare

Options available. Indicates the numeric value of the comparison type to use when calculating the substring. For numeric values, see the "Settings" section.

Set up

The compare parameter can have the following values:

Constant numerical description
Vbbinarycompare 0 performs binary comparisons.
vbTextCompare 1 performs a text comparison.

The following example uses the Split function to return an array from a string. function to make a textual comparison of the delimiters, returning all substrings. Description

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

again:
stng = "T1,t2,t3"
arr = Split (stng, ",")
MsgBox ar R (0)
MsgBox arr (1)
MsgBox arr (2)

Join function

Returns a string that is created by the concatenation of many substrings contained in the array.

Join (list[,delimiter])

Parameters

List

Required option. Contains a one-dimensional array of substrings to concatenate.

Delimiter

Options available. The character used to delimit substrings in the return string. If omitted, the null character ("") is used. If delimiter is a 0-length string, all items are listed in the same list without a delimiter.

The following example uses the join function to combine a myarray substring:

Dim MyString

Dim myarray (3)
myarray (0) = "Mr."
myarray (1) = "John"
myarray (2) = "Doe"
myarray (3) = "III"
Mystrin g = Join (myarray) ' MyString contains ' Mr John Doe III '.

again:
arr = Array ("T1", "T2", "T3")
MsgBox Join (arr, ",")

The above mainly on the VBS related functions are introduced, in the actual work will often encounter some special treatment, and are very practical, the following examples to illustrate:

Example one: comparison of one-dimensional arrays

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 two: comparison of two dimensional arrays

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 three: use loops to compare whether a value is contained in an array

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 the value of STR! The '
 Else
 MsgBox ' Arr does not contain the value of STR! "End
If

Example four: use a combination of functions to compare whether an array contains a value (optimization logic)

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) &G T 0 Then
 MsgBox "Arr contains the value of STR! The '
 Else
 MsgBox ' Arr does not contain the value of STR! "End
If

Flexible application of a variety of function commands, you can save a lot of redundant code, not only to streamline the code structure, provide execution efficiency.

Example five: Thearray as the return value of a function in VBS

Function Generaterandom ()

Dim myarray (2)
 Dim aa, Bb, cc
 Dim myvalue, Bbbase, ccbase 
 bbbase=array ("Beijing", "NewYork", "Copenhagen", " Paris, "London", "gothenborg") Ccbase=array ("The", "
 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 ' function 
Dim testarr 
testarr=generaterandom
MsgBox Testarr (0)
MsgBox Testarr (1) 
MsgBox Testarr (2)

Example six: array ordering

Function Fsortarray (Asortthisarray)

 Dim oarraylist, ielement Set oarraylist = CreateObject ("System.Collections.ArrayList") for ielement = 0 to Uboun D (Asortthisarray) Oarraylist.add Asortthisarray (ielement) Next oarraylist.sort Set fsortarray = OArrayList End Functi On Myarray=array (50,20,30) MsgBox myarray (0) MsgBox Fsortarray (myarray) (0) ' CreateObject ("System.Collections.ArrayLi St ") called Mscoree.dll, A. NET framework-related component. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.