发布新日志

  • 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专家视图里,运行得到期望结果。

  • 学习总结之——VBScript

    2008-08-04 11:41:57

    1、How to Put VBscrīpt Code in an HTML Document

    eg:

    <html>
    <head>
    </head>
    <body>
    <scrīpt type="text/vbscrīpt">
    document.write("Hello from VBscrīpt!")
    </scrīpt>
    </body>
    </html>
    To insert a scrīpt in an HTML document, use the <scrīpt> tag.
    Use the type attribute to define the scrīpting language.
    eg:<scrīpt type="text/vbscrīpt">
    Then comes the VBscrīpt: The command for writing some text 
    on a page is document.write
    eg:<document.write("Hello from VBscrīpt!")>
    The scrīpt ends;
    eg:</scrīpt>
    2、VBscrīpt Procedures
    We have two kinds of procedures: The Sub procedure and 
    the Function procedure.
    A Sub procedure:
    • is a series of statements, enclosed by the Sub and End Sub statements
    • can perform actions, but does not return a value
    • can take arguments that are passed to it by a calling procedure
    • without arguments, must include an empty set of parentheses ()

    eg:

    Sub mysub()
     some statements
    End Sub

    or

    Sub mysub(argument1,argument2)
     some statements
    End Sub 
    A Function procedure:
    • is a series of statements, enclosed by the Function
      and End Function statements
    • can perform actions and can return a value
    • can take arguments that are passed to it
      by a calling procedure
    • without arguments, must include an empty set of parentheses ()
    • returns a value by assigning a value to its name

    eg:

    Function myfunction()
     some statements
     myfunction=some value
    End Function

    or

    Function myfunction(argument1,argument2)
     some statements
     myfunction=some value
    End Function
    3、Call a Sub or Function Procedure
    When you call a Function in your code,you do like this:
    eg:name=findname()
    Here you call a Function called "findname",the Function 
    returns a value that will be stored in the variable"name".
    Or,you can do like this:
    eg:msgbox "You name is "& findname 
    Here you also call a Function called "findname",the Function 
    returns a value that will be display in the message box.
    When you call a Sub procedure you can use the Call statement,
    like this:
    eg:Call MyProc(argument)
    Or,you can omit the call statement,like this:
    eg:MyProc argument
    4、Conditional Statements
    If ...Then...Else
    You should use the If...Then...Else statement if you want to 
    • execute some code if a condition is true
    • select one of two blocks of code to execute

    If you want to execute only one statement when a condition is true,

    you can write the code on one line:

    eg: if i=10 then msgbox "Hello"

    There is no..else..in this syntax. You just tell the code to perform

    one action if the condition is true(in this case if i=10).

    If you want to execute more than one statement when a condition is

    true you must put each statement on separate lines and end the

    statement with the keyword "End If":

    eg:

    if i=10 Then

       msgbox "Hello"

       i=i+1

    end if

    There is no..else..in this syntax either.You jiust tell the code

     

    5、Looping Statements

    For...Next Loop

    You can use a For...Next statement to run a block of code,

    when you know how many repetitions you want.

    You can use a counter variable that increases or decreases with

    each repetition of the loop, like this:

    eg: For i=1 to 10
        some code
        Next
    The For Statement specifies the counter variable(i)and its start
    and end values.The Next statement increaes the counter variable(i)
    by one.
    Step Keyword
    Using the step keyword,you can increase or decrease the counter
    variable by the value you specify.
    In the example below,the counter variable(i)is increased by two
    each time the loop repeats
    eg: For i=2 to 10 step 2
        some code
        next
    To decrease the counter variableyou must use a negative step value.
    You must specify an end value that is less than zhe start value.
    In the example below,the counter variable(i) is decrease by two
    each time the loop repeats.
    eg: For i=10 to 2 step -2
        some code
        Next
    Exit a For...Next
    You can exit a For...Next statement with the Exit for keywords.

    For Each...Next Loop

     

    A For Each...Next loop repeats a block of code for each item in a

     

    collection, or for each element of an array.

     

    eg:dim cars(2)

    cars(0)="Volvo"

    cars(1)="Saab"

    cars(2)="BMW" For Each x in cars

    document.write(x & "<br />")

    Next

     

    Do...Loop

     

    You can use Do...Loop statements to run a block of code

     

    when you do not know how many repetitions you want.

     

    The block of code is repeated while a condition is

     

    true or until a condition becomes true.

     

    Repeating Code While a Condition is True

     

    You use the While keyword to check a condition

     

    in a Do...Loop statement.

     

    eg:

    Do While i>10
    
      some code
    
    Loop
     

    If i equals 9, the code inside the loop above will never be executed.

     

    eg:Do some code

    Loop While i>10

     

    The code inside this loop will be executed at least one time,

     

    even if i is less than 10.

     

    Repeating Code Until a Condition Becomes True

     

    You use the Until keyword to check a condition in a Do...Loop

    statement.

     

    eg:Do Until i=10

    some code

    Loop

     

    If i equals 10, the code inside the loop will never be executed.

    eg:Do
    
        some code
    
       Loop Until i=10
     

    The code inside this loop will be executed at least one time,

     

    even if i is equal to 10.

     

    Exit a Do...Loop

     

    You can exit a Do...Loop statement with the Exit Do keyword.

    eg:Do Until i=10
    
        i=i-1
    
        If i<10 Then Exit Do
    
       Loop
     

    The code inside this loop will be executed as long as i is

     

    different from 10, and as long as i is greater than 10.

我的栏目

数据统计

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

RSS订阅

Open Toolbar