Sometimes, data is to be stored in an Array. For this, you can use Array Variables. Basically, this is a variable that can be accessed using an index to get a specific value. Array Variables can have one or more dimensions, each having 1 or more elements. All indexes range from 0 to (ElementCount - 1).
The following statements/functions are related to Array Variables
Dim Variable([Index] [,Index] ...) | To define a Variable. Also used to define Array Variables. Required. |
ReDim [Preserve] Variable([Index] [,Index] ...) | To re-define an Array Variable. Can be used to alter the dimensions/sizes. Can be used with the Preserve keywords to keep the data while changing the number of elements. |
Erase Variable | To erase the data in a variable while keeping the dimensions/sizes. |
Ubound(Variable [, Dimension#]) | To get the Upper Boundary of the given dimension. If no dimension is specified then the Upper Boundary of the first dimension is returned. The UBound is the number of elements - 1, so an empty array(no elements) returns UBound -1. |
Lbound(Variable [, Dimension#]) | To get the Lower Boundary of the given dimension. Always returns 0, for Polar Script (just like VBscript) does not allow to set custom boundaries. |
DimensionCount(Variable) | To get the number of dimensions for an Array variable. Note that the dimenstions are One-based. Also returns 1 for an empty array. |
Join(ArrayVariable [, SepChar = " "]) As String | Will create a string of the joined elements of a 1-dimensional array. |
Split(StringVariable [, SepChar = " "] [, Count = -1] [, CompareMode = 0]) As String() | Splits a string into an array of strings searching the separation character (that will be removed). Limits the number of elements to the specified Count (use -1 for unlimited). CompareMode is 0 for binary compare (vbBinaryCompare) and 1 for text compare (vbTextCompare) |
Filter(ArrayVariable , Find [, Include = True] [, CompareMode = 0] [, UsePatternMatching = False] ) As String() | Returns a 1-dimensional array of strings by filtering the given 1-dimensional array. It filters on the Find-parameter. By default it Includes matches, but by setting Include=False, it will exclude the matches instead. The pattern matching will use the given Find and use it as the Like operator. |
IsArray(Variable) | Returns a boolean indicating if the given variable is an Array. |
Dim mMyData() As String
Public Function RegisterElement(pElement As String)
Redim Preserve mMyData(UBound(mMyData) + 1) 'Add an extra element
mMyData(UBound(mMyData)) = pElement 'Store at the last position
End Function
An example for a two dimensional array could look like this:
Dim mDiceCount(5, 5) As Long
Public Function RegisterDiceRoll(pDice1 As Long, pDice2 As Long)
mDiceCount(pDice1 - 1, pDice2 - 1) = mDiceCount(pDice1 - 1, pDice2 - 1) + 1
End Function
Public Function ClearDiceRolls()
Erase mDiceCount
End Function