VBScript:数组

什么是数组?

我们非常清楚变量是存储值的容器。有时,开发人员可以一次在单个变量中保留多个值。当一系列值存储在单个变量中时,则称为数组变量

数组声明

声明数组的方式与声明变量的方式相同,只不过声明数组变量使用括号。在下面的示例中,括号中提到了数组的大小。

'Method 1 : Using Dim
Dim arr1() 'Without Size

'Method 2 : Mentioning the Size
Dim arr2(5) 'Declared with size of 5

'Method 3 : using 'Array' Parameter
Dim arr3
arr3 = Array("apple","Orange","Grapes")
  • 虽然Array size表示为5,但由于数组索引从ZERO开始,它可以容纳6个值。
  • 数组索引不能为负。
  • VBScript数组可以在数组中存储任何类型的变量。因此,数组可以在单个数组变量中存储整数,字符串或字符。

将值分配给数组

通过针对要分配的每个值指定数组索引值,将值分配给数组。它可以是一个字符串。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim arr(5)
         arr(0) = "1"            'Number as String
         arr(1) = "VBScript"     'String
         arr(2) = 100            'Number
         arr(3) = 2.45           'Decimal Number
         arr(4) = #10/07/2020#   'Date
         arr(5) = #12.45 PM#     'Time

         document.write("Value stored in Array index 0 : " & arr(0) & "<br />")
         document.write("Value stored in Array index 1 : " & arr(1) & "<br />")
         document.write("Value stored in Array index 2 : " & arr(2) & "<br />")
         document.write("Value stored in Array index 3 : " & arr(3) & "<br />")
         document.write("Value stored in Array index 4 : " & arr(4) & "<br />")
         document.write("Value stored in Array index 5 : " & arr(5) & "<br />")

      </script>
   </body>
</html>

结果:

Value stored in Array index 0 : 1
Value stored in Array index 1 : VBScript
Value stored in Array index 2 : 100
Value stored in Array index 3 : 2.45
Value stored in Array index 4 : 7/10/2020
Value stored in Array index 5 : 12:45:00 PM

多维数组

数组不仅限于单个维度,而且最多可以包含60个维度。二维数组是最常用的数组。

在下面的示例中,声明了具有3行4列的多维数组。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim arr(2,3)	' Which has 3 rows and 4 columns
         arr(0,0) = "Apple" 
         arr(0,1) = "Orange"
         arr(0,2) = "Grapes"           
         arr(0,3) = "pineapple" 
         
         arr(1,0) = "cucumber"           
         arr(1,1) = "beans"           
         arr(1,2) = "carrot"           
         arr(1,3) = "tomato"    
         
         arr(2,0) = "potato"             
         arr(2,1) = "sandwitch"            
         arr(2,2) = "coffee"             
         arr(2,3) = "nuts"            
                  
         document.write("Value in Array index 0,1 : " &  arr(0,1) & "<br />")
         document.write("Value in Array index 2,2 : " &  arr(2,2) & "<br />")

      </script>
   </body>
</html>

结果:

Value stored in Array index : 0 , 1 : Orange
Value stored in Array index : 2 , 2 : coffee

Redim声明

ReDim语句用于声明动态数组变量以及分配或重新分配存储空间。

ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
  • Preserve-一个可选参数,用于在更改最后一个维度的大小时将数据保留在现有数组中。
  • varname-一个必需的参数,表示变量的名称,应遵循标准变量命名约定。
  • 下标-一个Required参数,它指示数组的大小。

在下面的示例中,一个数组已被重新定义,然后在更改数组的现有大小时保留其值。

–在调整比原来小的数组的大小时,消除的元素中的数据将丢失。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim a()
         i = 0
         redim a(5)
         a(0) = "XYZ"
         a(1) = 41.25
         a(2) = 22
           
         REDIM PRESERVE a(7)
         For i = 3 to 7
         a(i) = i
         Next
           
         'to Fetch the output
         For i = 0 to ubound(a)
            Msgbox a(i)
         Next
      </script>
   </body>
</html>

当我们将以上脚本另存为HTML并在浏览器中执行时,它会产生以下结果。

XYZ
41.25
22
3
4
5
6
7

数组方法

VBScript中有各种内置函数,可帮助开发人员有效地处理数组。下面列出了与数组结合使用的所有方法。请单击方法名称以了解详细信息。

功能描述
LBound一个函数,返回一个整数,该整数对应于给定数组的最小下标。
UBound一个函数,该函数返回一个整数,该整数对应于给定数组的最大下标。
Split一个Function,它返回一个包含指定数量的值的数组。根据定界符拆分。
Join一个函数,它返回一个字符串,该字符串在数组中包含指定数目的子字符串。这与拆分方法完全相反。
Filter一个函数,该函数返回基于零的数组,该数组包含基于特定过滤条件的字符串数组的子集。
IsArray一个函数,它返回一个布尔值,该布尔值指示输入变量是否为数组。
Erase一个函数,它为数组变量恢复分配的内存。

作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1184.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年12月7日 下午5:59
下一篇 2020年12月8日 上午10:46

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注