VbScript--数组的定义及应用

上一篇 / 下一篇  2009-04-02 16:21:56 / 个人分类:学习总结

静态数组的定义:

对于数值类型的数组可以进行:

Dim A,i

A=array(1,2,3,4)

Or

Dim A(3)

A(0)=1

A(1)=2

A(2)=3

A(3)=4

对于字符串类型的数组定义:

Dim A(3)

A(0)="gdfhfj"

A(1)="ghyt"

A(2)="fdh"

A(3)="dhdfh"

Dim A(3,4)

在二位数组中,括号中第一个数字表示行的数目,第二行数字表示列的数目。

动态数据的定义:

在最初声明时使用Dim语句或者ReDim语句。但是对于动态数组,括号中不包含任何数字。

Dim MyArray()

ReDim MyArray()

在使用动态数组,必须随后使用ReDim确定维数和每一维的大小。

ReDim MyArray(25)

....

ReDim Preserve MyArray(30)

重新调整动态数组大小的次数是没有任何限制的,但是应注意:将数组的大小调小时,将会丢失被删除元素的数据

 

数组相关的函数:

LBound函数

返回指定数组维的最小可用下标

语法: LBound(arrayname[,dimension])

arrayname:数组变量名,遵循标准变量命名约定

dimension:Optional,指明要返回哪一维下界的整数。使用1表示第一维,2表示第二维。如果略       dimension参数,默认值为1.所有维默认下界均为0

UBound函数

返回指定数组维数的最大可用下标

语法: LBound(arrayname[,dimension])

arrayname:数组变量名,遵循标准变量命名约定

dimension:Optional,指明要返回哪一维下界的整数。使用1表示第一维,2表示第二维。如果略       dimension参数,默认值为1.

实例1:

'*********************************************************
' 目的:在UserList数组中
' 定位指定用户的首次出现。
' 输入:strUserList():要搜索的用户列表。
' strTargetUser:要搜索的用户名。
' 返回:索引strUserList数组中
' strTargetUser的首次出现。
' 如果找不到目标用户,则返回-1。'*********************************************************

'定义变量

Dim strUserList()'动态定义数组
Dim strTargetUser
Dim endresult
ReDim strUserList(4)

' 给数组赋值
strUserList(0)="dggseh"
strUserList(1)="dgger"
strUserList(2)="gfhrth"
strUserList(3)="detg"
strUserList(4)="dger"

'给变量赋值
strTargetUser="detg"
msgbox (Ubound(strUserList))'显示数组的最大维数
endresult= IntFindUser(strUserList,strTargetUser)'调用函数
msgbox(endresult)'输出结果

' 函数主体

Function intFindUser(strUserList(), strTargetUser)
   Dim i ' Loop counter
   Dim num
   Dim binFound ' 找到目标标志
   msgbox(strTargetUser)
   binFound=false
   intFindUser=-1
   i=0
  msgbox(Ubound(strUserList))
   Do While i<=Ubound(strUserList) and Not binFound
    If strUserList(i)=strTargetUser Then
     binFound=true
     intFindUser=i
     msgbox(intFindUser)
    End If
    i=i+1 '递增循环计数器
   Loop
End Function

UBound 函数实例:

Dim A(3,4,5)
A(0,0,0)=111
A(3,4,5)=345
msgbox(A(0,0,0))
msgbox(A(3,4,5))
msgbox(UBound(A,1))
msgbox(UBound(A,2))
msgbox(UBound(A,3))

以上实例可以把脚本拷到QTP专家视图里,运行得到期望结果。


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 6054
  • 日志数: 4
  • 文件数: 1
  • 建立时间: 2008-07-29
  • 更新时间: 2009-04-02

RSS订阅

Open Toolbar