固定長配列

通常、固定長配列は、コンパイル時に数が判明しており、アプリケーションの実行中には変更しない要素集合を操作するために使用します。例えば、会社の駐車場の駐車スペースを、階数、区画、駐車スペース番号によって従業員名とマッチングするために、固定長配列を使用できます。

また、駐車場が 3 階建てで、各階が 4 つの区画に分割されており、各区画には 10 個の駐車スペースがあるとします。この 120 個の駐車スペースと、そのスペースを割り当てられた従業員に関する情報を整理するには、2 つの方法が考えられます。

まず第 1 の方法では、2 次元配列を使用します。配列には 480 個の要素があり、それぞれの要素が 120 個の駐車スペースのそれぞれに関する 4 つの情報を表します。2 つのサブスクリプトを使用してこの配列の特定の要素を参照する場合、最初のサブスクリプトが駐車スペースを識別し、2 番目のサブスクリプトが階、区画、駐車スペース番号、またはそれに割り当てられている従業員名を識別します。

Dim empSpacesA(1 To 120, 1 To 4) As String
empSpacesA(1,1) = "Floor 1"
empSpacesA(1,2) = "Section 1"
empSpacesA(1,3) = "Space 1"
empSpacesA(1,4) = "Maria Jones"
empSpacesA(2,1) = "Floor 1"
empSpacesA(2,2) = "Section 1"
empSpacesA(2,3) = "Space 2"
empSpacesA(2,4) = "Fred Smith"
' And so on down to the last space.
empSpacesA(120,1) = "Floor 3"
empSpacesA(120,2) = "Section 4"
empSpacesA(120,3) = "Space 10"
empSpacesA(120,4) = "Sal Piccio"
' Print information about Fred Smith's space.
Print empSpacesA(2,1) & " " & empSpacesA(2,2) & " " _
   empSpacesA(2,3) & " " empSpacesA(2,4)
' Output: Floor 1 Section 1 Space 2 Fred Smith

2 つ目の方法では、3 次元配列を使用します。配列には 120 の要素があり、各要素にはそれぞれの駐車スペースに割り当てられた従業員名が入っています。この配列の特定の要素を識別する 3 つのサブスクリプトは、その従業員が割り当てられている階、セクション、駐車スペースに対応します。

Dim empSpacesB(1 To 3, 1 To 4, 1 To 10) As String
empSpacesB(1,1,1) = "Maria Jones"
empSpacesB(1,1,2) = "Fred Smith"
' And so on down to the last space.
empSpacesB(3,4,10) = "Sal Piccio"
' Print information about Fred Smith's space.
Print "Floor 1 Section 1 Space 2 " & empSpacesB(1,1,2)
' Output: Floor 1 Section 1 Space 2 Fred Smith

このような 2 つの方法では、要素が String 型の多次元固定長配列を宣言します。どちらの配列にも駐車スペースに関する同量の情報を含む一方で、次元や要素数が異なり、各駐車スペースの情報の入力と取得の際には、異なる方針を用いる必要があります。

固定長配列を宣言する

固定長配列を宣言する場合、データ型、数、保持する要素の編成を指定します。宣言の As dataType 節で、配列要素のデータ型を指定します。

' Declare a one-dimensional array of strings.
Dim aStringArray(1 To 10) As String
' Declare a two-dimensional array of Variants.
Dim myVarArrayV(1 To 10, 1 To 10) As Variant

配列が保持する値が LotusScript® で認識されるスカラーデータ型の 1 つに属する場合は、As dataType を省略し、代わりに配列名の後ろに適切なデータ型接尾辞を付けられます。

' Declare a one-dimensional array of strings.
Dim aStringArray$(1 To 10)
' Declare a two-dimensional array of integers.
Dim anIntArray%(1 To 10, 1 To 10)

接尾辞と As dataType 節の両方とも省略すると、LotusScript は配列名が適用可能な Deftype ステートメントで処理されるかどうかを調べます。処理される場合、配列要素が適切なデータ型として定義されます。処理されない場合、Variant 型として定義されます。

DefInt A-C
' Declare an array of integers.
Dim arrayOfInts(1 To 10)
' Declare an array of Variants.
Dim otherArrayV(1 To 10) 

配列の要素数と次元数を (範囲リストに整理して) 指定します。配列の次元の下限と上限は、-32768~32767 の範囲内の数値の定数です。ただし、プロシージャにローカルな固定長配列が 32 KB を超える記憶域を占有できないという制約があるので、多次元配列の下限と上限の範囲はこれより小さくなります。配列に必要なメモリは、配列のサイズと配列要素に必要な記憶域によって異なります。配列のサイズは、その中の要素のサイズの合計です。これは、すべての次元のサイズの積となります。

以下に例を示します。

Dim arrayOfSingles(1 To 5, 1 To 10, 1 To 2) As Single

次元の長さは 5、10、2 なので、arrayOfSingles は 100 個の要素を保持します。Single 型の 1 つの値には 4 バイト必要なので、全要素に必要な実際の記憶域は、400 バイトです。

以下に例を示します。

Dim myStats(1980 To 1983, 1 To 4, -2 To 2) As Currency

次元の長さは 4、4、5 (1980, 1981, 1982, 1983; 1, 2, 3, 4; -2, -1, 0, 1, 2) なので、合計で 80 個の要素を持ち、各要素には 8 バイトの記憶域が必要です。したがって、myStats を格納するために必要なメモリ容量は、640 バイトになります。

このような配列を myStats として使用すると、1980 年から 1983 年までの年度の各四半期について、釣り鐘型の曲線を描いて分布する値を保持できます。1 から 4、1 から 4、1 から 5 の代わりに 1980 から 1983、1 から 4、-2 から 2 という範囲のサブスクリプトを使用するのは、分かりやすく直観的に配列の値を入力および取得できるようにするためです。1982 年の第 2 四半期について、曲線に相当する値を入力するには、次のようにステートメントを使用します。

myStats(1982, 2, -2) = 123.456

この例では、次元の下限値は必ずしも 1 である必要はないことを示していますが、1 かゼロにしておくと通常は便利です。LotusScript では、モジュール内に適切な Option Base ステートメントを含めることにより、モジュール内で宣言するすべての配列の次元に対し、既定値の下限を 1 またはゼロに設定できます。Option Base 0 は LotusScript 言語の既定値ですが、使用する製品によっては別の値に設定されている場合もあります。これはユーザーが無効にできます。

以下に例を示します。

Option Base 0
' Declare a 120 x 4 array, both of whose dimensions
' are zero origin. This is the same as saying 
' Dim empSpacesA(0 To 119, 0 To 3) As String
Dim empSpacesA(119, 3) As String

' Declare a 3 x 4 x 10 array, all of whose dimensions
' are zero origin. This is the same as saying
' Dim EmpSpacesB(0 To 2, 0 To 3, 0 To 9) As String 
Dim empSpacesB(2, 3, 9) As String 

もう 1 つ例を示します。

Option Base 1
' Declare a 120 x 4 array, both of whose dimensions
' are one origin. This is the same as saying 
' Dim empSpacesA(1 To 120, 1 To 4) As String
Dim empSpacesA(120, 4) As String

' Declare a 3 x 4 x 10 array, all of whose dimensions
' are one origin. This is the same as
' Dim EmpSpacesB(1 To 3, 1 To 4, 1 To 10) As String
Dim empSpacesB(3, 4, 10) As String

宣言では、下限値の明示的な指定と暗黙的な指定を混在させられます。

Option Base 0
Dim myStats(3, 1 To 2, -2 To 2) As Currency
' The first dimension of this 4 x 2 x 5 array is 0 To 3.

Dim arrayOfSingles(1 To 5, 9, 1) As Single
' The second and third dimensions of this 5 x 10 x 2 array
' are 0 To 9 and 0 To 1, respectively. 

LBound 関数を使用して、次元の下限値を調べます。構文は次のとおりです。

LBound ( arrayName [ , dimension ] )

ここで、arrayName は配列の名前で、dimension は調べる対象となる下限値を持つ次元を表す整数です。dimension の既定値は 1 です。例えば、次のようになります。

Option Base 1 
Dim myStats(1980 To 1983, 2, -2 To 2) As Currency
Print LBound(myStats)
' Output: 1980 (the lower bound of the first dimension).
Print LBound(myStats, 2)
' Output: 1 (the lower bound of the second dimension). 

UBound 関数を使用すれば、次元の上限値を調べられます。

配列の要素を参照する

配列内の値の代入方法または参照方法は、配列要素のデータ型によって異なります。ここでは、スカラーデータ型の配列要素の参照方法、値を代入する方法を説明します。

次の形式のステートメントを使用して、配列内の要素にスカラー値を代入します。

arrayName( S1, S2, S3,... ) = value

ここで arrayName は配列の名前で、S1, S2, S3,... はサブスクリプト (配列の各次元に 1 つ)、valueS1, S2, S3,.. によって定義される配列内の位置にある要素に代入する値です。以下に例を示します。

Option Base 1
Dim empSpacesB(3,4,10) As String
empSpacesB(1,1,1) = "Maria Jones"
empSpacesB(1,1,2) = "Fred Smith"

または、

Dim empSpacesA(120,4) As String
Dim counter As Integer
Dim LB1 As Integer
Dim LB2 As Integer
' Get lower bound of first dimension.
LB1% = LBound(empSpacesA, 1)
' Get lower bound of second dimension.
LB2% = LBound(empSpacesA, 2)
' For the first 40 elements in the first dimension,
' assign the value "Floor 1" to the first element
' in the second dimension; for the next 40 elements
' in the first dimension, assign the value "Floor 2"
' to the first element in the second dimension; and
' for the last 40, assign the value "Floor 3".
For counter% = LB1% to LB1% + 39
     empSpacesA(counter%, LB2%) = "Floor 1"
     empSpacesA(counter% + 40, LB2%) = "Floor 2"
     empSpacesA(counter% + 80, LB2%) = "Floor 3"
Next

次の例にあるように、配列のスカラー要素の値は、要素のサブスクリプトによって参照します。この例では、従業員が割り当てられていない駐車スペースを検索しています。

Option Base 1
Dim empSpacesB(3,4,10) As String
' Declare three String variables the quickest way
' to hold values for floor, section, and space.
Dim Flo$, Sec$, Spa$
' Declare six Integer variables the quickest way
' to hold values for the lower and upper bounds
' of the dimensions of empSpacesB for easy reference.
Dim LB1%, LB2%, LB3%, UB1%, UB2%, UB3%
' Initialize the array. Typically you do this by reading
' the data from a file rather than by hard-coding the
' values.
empSpacesB(1,1,1) = "Maria Jones"
empSpacesB(1,1,2) = ""
empSpacesB(1,1,3) = "Joe Smith"
' And so on down to the last space.
empSpacesB(3,4,10) = "Sal Piccio"
' Assign the lower and upper bounds of each dimension
' of empSpacesB to a variable.
LB1% = LBound(empSpacesB, 1)
LB2% = LBound(empSpacesB, 2)
LB3% = LBound(empSpacesB, 3)
UB1% = UBound(empSpacesB, 1)
UB2% = UBound(empSpacesB, 2)
UB3% = UBound(empSpacesB, 3)

' Loop through all the array elements and print
' the floor, section, and location of each space
' that has the empty string--that is, no employee name--
' as its value. Convert the floor, section, and space
' numbers to strings by calling the cStr function and
' passing it the appropriate subscript.
For counter1% = LB1% to UB1%
  For counter2% = LB2% to UB2%
    For counter3% = LB3% to UB3%
      If empSpacesB(counter1%, counter2%, counter3%) = "" Then
         Flo$ = "Floor " & cStr(counter1%) & " "
         Sec$ = "Section " & cStr(counter2%) & " "
         Spa$ = "Space " & cStr(counter3%) & " "
         Print Flo$ & Sec$ & Spa$ & "is empty."
      End If
    Next
  Next
Next