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")
  • 雖然陣列大小指定為 5,但它可以容納 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/2013#   '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>

當以上程式碼儲存為 .HTML 並使用 Internet Explorer 執行時,會產生以下結果:

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/2013
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>

當以上程式碼儲存為 .HTML 並使用 Internet Explorer 執行時,會產生以下結果:

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 - 一個必需引數,表示變數名,應遵循標準變數命名約定。

  • subscripts - 一個必需引數,表示陣列的大小。

示例

在下面的示例中,一個數組被重新定義,然後在更改陣列的現有大小時保留值。

注意 - 將陣列調整為小於其原始大小,則已消除元素中的資料將丟失。

<!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 並使用 Internet Explorer 執行時,會產生以下結果。

XYZ
41.25
22
3
4
5
6
7

陣列方法

VBScript 中有各種內建函式可以幫助開發人員有效地處理陣列。下面列出了所有與陣列一起使用的方法。請單擊方法名稱以詳細瞭解。

函式 描述
LBound 一個函式,它返回一個整數,該整數對應於給定陣列的最小下標。
UBound 一個函式,它返回一個整數,該整數對應於給定陣列的最大下標。
Split 一個函式,它返回一個包含指定數量值的陣列。基於分隔符進行拆分。
Join 一個函式,它返回一個包含陣列中指定數量子字串的字串。這是 Split 方法的完全相反的函式。
Filter 一個函式,它返回一個基於零的陣列,該陣列包含基於特定篩選條件的字串陣列的子集。
IsArray 一個函式,它返回一個布林值,指示輸入變數是否為陣列。
Erase 一個函式,它回收陣列變數的已分配記憶體。
廣告