Vbscript-arrays

提供:Dev Guides
移動先:案内検索

VBScript-配列

配列とは何ですか?

変数は値を格納するコンテナであることをよく知っています。 開発者は、一度に1つの変数に複数の値を保持できる場合があります。 一連の値が単一の変数に格納される場合、それは「配列変数」と呼ばれます。

配列宣言

配列は、変数の宣言と同じ方法で宣言されますが、配列変数の宣言では括弧が使用されます。 次の例では、配列のサイズが括弧内に記載されています。

'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次元を持つことができます。 2次元配列は、最も一般的に使用されるものです。

次の例では、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には、開発者が配列を効果的に処理するのに役立つさまざまな組み込み関数があります。 配列と組み合わせて使用​​されるすべてのメソッドを以下にリストします。 詳細を知るには、メソッド名をクリックしてください。

Function Description
LBound A Function, which returns an integer that corresponds to the smallest subscript of the given arrays.
UBound A Function, which returns an integer that corresponds to the Largest subscript of the given arrays.
Split A Function, which returns an array that contains a specified number of values. Splitted based on a Delimiter.
Join A Function, which returns a String that contains a specified number of substrings in an array. This is an exact opposite function of Split Method.
Filter A Function, which returns a zero based array that contains a subset of a string array based on a specific filter criteria.
IsArray A Function, which returns a boolean value that indicates whether or not the input variable is an array.
Erase A Function, which recovers the allocated memory for the array variables.