Difference between revisions of "Documentation/BASIC Guide/Arrays"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 14: Line 14:
 
An array declaration is similar to that of a simple variable declaration. However, unlike the variable declaration, the array name is followed by parentheses which contain the specifications for the number of elements. The expression
 
An array declaration is similar to that of a simple variable declaration. However, unlike the variable declaration, the array name is followed by parentheses which contain the specifications for the number of elements. The expression
  
Dim MyArray(3)
+
<source lang="oobas">
 +
Dim MyArray(3)
 +
</source>
  
 
declares an array that has four variables of the variant data type, namely <tt>MyArray(0)</tt>, <tt>MyArray(1)</tt>, <tt>MyArray(2)</tt>, and <tt>MyArray(3)</tt>.
 
declares an array that has four variables of the variant data type, namely <tt>MyArray(0)</tt>, <tt>MyArray(1)</tt>, <tt>MyArray(2)</tt>, and <tt>MyArray(3)</tt>.
Line 20: Line 22:
 
You can also declare type-specific variables in an array. For example, the following line declares an array with four integer variables:
 
You can also declare type-specific variables in an array. For example, the following line declares an array with four integer variables:
  
Dim MyInteger(3) As Integer
+
<source lang="oobas">
 +
Dim MyInteger(3) As Integer
 +
</source>
  
 
In the previous examples, the index for the array always begins with the standard start value of zero. As an alternative, a validity range with start and end values can be specified for the data field declaration. The following example declares a data field that has six integer values and which can be addressed using the indexes 5 to 10:
 
In the previous examples, the index for the array always begins with the standard start value of zero. As an alternative, a validity range with start and end values can be specified for the data field declaration. The following example declares a data field that has six integer values and which can be addressed using the indexes 5 to 10:
  
Dim MyInteger(5 To 10)
+
<source lang="oobas">
 +
Dim MyInteger(5 To 10)
 +
</source>
  
 
The indexes do not need to be positive values. The following example also shows a correct declaration, but with negative data field limits:
 
The indexes do not need to be positive values. The following example also shows a correct declaration, but with negative data field limits:
  
Dim MyInteger(-10 To -5)
+
<source lang="oobas">
 +
Dim MyInteger(-10 To -5)
 +
</source>
  
 
It declares an integer data field with 6 values that can be addressed using the indexes -10 to -5.
 
It declares an integer data field with 6 values that can be addressed using the indexes -10 to -5.
Line 44: Line 52:
 
The start index of a data field usually begins with the value 0. Alternatively, you can change the start index for all data field declarations to the value 1 by using the call:
 
The start index of a data field usually begins with the value 0. Alternatively, you can change the start index for all data field declarations to the value 1 by using the call:
  
Option Base 1
+
<source lang="oobas">
 +
Option Base 1
 +
</source>
  
 
The call must be included in the header of a module if you want it to apply to all array declarations in the module. However, this call does not affect the UNO sequences that are defined through the {{OOo}} API whose index always begins with 0. To improve clarity, you should avoid using Option Base 1.
 
The call must be included in the header of a module if you want it to apply to all array declarations in the module. However, this call does not affect the UNO sequences that are defined through the {{OOo}} API whose index always begins with 0. To improve clarity, you should avoid using Option Base 1.
Line 50: Line 60:
 
The number of elements in an array is not affected if you use <tt>Option Base 1</tt>, only the start index changes. The declaration
 
The number of elements in an array is not affected if you use <tt>Option Base 1</tt>, only the start index changes. The declaration
  
Option Base 1
+
<source lang="oobas">
' ...
+
Option Base 1
Dim MyInteger(3)
+
' ...
 +
Dim MyInteger(3)
 +
</source>
  
 
creates 4 integer variables which can be described with the expressions <tt>MyInteger(1)</tt>, <tt>MyInteger(2)</tt>, <tt>MyInteger(3)</tt>, and <tt>MyInteger(4)</tt>.
 
creates 4 integer variables which can be described with the expressions <tt>MyInteger(1)</tt>, <tt>MyInteger(2)</tt>, <tt>MyInteger(3)</tt>, and <tt>MyInteger(4)</tt>.
Line 62: Line 74:
 
In addition to single dimensional data fields, {{OOo}} Basic also supports work with multi-dimensional data fields. The corresponding dimensions are separated from one another by commas. The example
 
In addition to single dimensional data fields, {{OOo}} Basic also supports work with multi-dimensional data fields. The corresponding dimensions are separated from one another by commas. The example
  
Dim MyIntArray(5, 5)
+
<source lang="oobas">
 +
Dim MyIntArray(5, 5)
 +
</source>
  
 
defines an integer array with two dimensions, each with 6 indexes (can be addressed through the indexes 0 to 5). The entire array can record a total of 6 x 6 = 36 integer values.
 
defines an integer array with two dimensions, each with 6 indexes (can be addressed through the indexes 0 to 5). The entire array can record a total of 6 x 6 = 36 integer values.
Line 72: Line 86:
 
The previous examples are based on data fields of a specified dimension. You can also define arrays in which the dimension of the data fields dynamically changes. For example, you can define an array to contain all of the words in a text that begin with the letter A. As the number of these words is initially unknown, you need to be able to subsequently change the field limits. To do this in {{OOo}} Basic, use the following call:
 
The previous examples are based on data fields of a specified dimension. You can also define arrays in which the dimension of the data fields dynamically changes. For example, you can define an array to contain all of the words in a text that begin with the letter A. As the number of these words is initially unknown, you need to be able to subsequently change the field limits. To do this in {{OOo}} Basic, use the following call:
  
ReDim MyArray(10)
+
<source lang="oobas">
 +
ReDim MyArray(10)
 +
</source>
  
 
{{Documentation/Note|Unlike VBA, where you can only dimension dynamic arrays by using <tt>Dim MyArray()</tt>, {{OOo}} Basic lets you change both static and dynamic arrays using <tt>ReDim</tt>.}}
 
{{Documentation/Note|Unlike VBA, where you can only dimension dynamic arrays by using <tt>Dim MyArray()</tt>, {{OOo}} Basic lets you change both static and dynamic arrays using <tt>ReDim</tt>.}}
Line 78: Line 94:
 
The following example changes the dimension of the initial array so that it can record 11 or 21 values:
 
The following example changes the dimension of the initial array so that it can record 11 or 21 values:
  
Dim MyArray(4) As Integer ' Declaration with five elements  
+
<source lang="oobas">
' ...
+
Dim MyArray(4) As Integer ' Declaration with five elements  
ReDim MyArray(10) As Integer ' Increase to 11 elements
+
' ...
' ...  
+
ReDim MyArray(10) As Integer ' Increase to 11 elements
ReDim MyArray(20) As Integer ' Increase to 21 elements
+
' ...  
 +
ReDim MyArray(20) As Integer ' Increase to 21 elements
 +
</source>
  
 
When you reset the dimensions of an array, you can use any of the options outlined in the previous sections. This includes declaring multi-dimensional data fields and specifying explicit start and end values. When the dimensions of the data field are changed, all contents are lost. If you want to keep the original values, use the <tt>Preserve</tt> command:
 
When you reset the dimensions of an array, you can use any of the options outlined in the previous sections. This includes declaring multi-dimensional data fields and specifying explicit start and end values. When the dimensions of the data field are changed, all contents are lost. If you want to keep the original values, use the <tt>Preserve</tt> command:
  
Dim MyArray(10) As Integer ' Defining the initial  
+
<source lang="oobas">
' dimensions
+
Dim MyArray(10) As Integer ' Defining the initial  
' ...  
+
' dimensions
ReDim Preserve MyArray(20) As Integer ' Increase in  
+
' ...  
' data field, while
+
ReDim Preserve MyArray(20) As Integer ' Increase in  
' retaining content
+
' data field, while
 +
' retaining content
 +
</source>
  
 
When you use <tt>Preserve</tt>, ensure that the number of dimensions and the type of variables remain the same.
 
When you use <tt>Preserve</tt>, ensure that the number of dimensions and the type of variables remain the same.

Revision as of 13:02, 2 April 2008


In addition to simple variables (scalars), Apache OpenOffice Basic also supports arrays (data fields). A data field contains several variables, which are addressed through an index.

Simple Arrays

An array declaration is similar to that of a simple variable declaration. However, unlike the variable declaration, the array name is followed by parentheses which contain the specifications for the number of elements. The expression

Dim MyArray(3)

declares an array that has four variables of the variant data type, namely MyArray(0), MyArray(1), MyArray(2), and MyArray(3).

You can also declare type-specific variables in an array. For example, the following line declares an array with four integer variables:

Dim MyInteger(3) As Integer

In the previous examples, the index for the array always begins with the standard start value of zero. As an alternative, a validity range with start and end values can be specified for the data field declaration. The following example declares a data field that has six integer values and which can be addressed using the indexes 5 to 10:

Dim MyInteger(5 To 10)

The indexes do not need to be positive values. The following example also shows a correct declaration, but with negative data field limits:

Dim MyInteger(-10 To -5)

It declares an integer data field with 6 values that can be addressed using the indexes -10 to -5.

There are three limits that you must observe when you define data field indexes:

  • The smallest possible index is -32768.
  • The largest possible index is 32767.
  • The maximum number of elements (within a data field dimension) is 16368.

Template:Documentation/Note

Specified Value for Start Index

The start index of a data field usually begins with the value 0. Alternatively, you can change the start index for all data field declarations to the value 1 by using the call:

Option Base 1

The call must be included in the header of a module if you want it to apply to all array declarations in the module. However, this call does not affect the UNO sequences that are defined through the Apache OpenOffice API whose index always begins with 0. To improve clarity, you should avoid using Option Base 1.

The number of elements in an array is not affected if you use Option Base 1, only the start index changes. The declaration

Option Base 1
' ...
Dim MyInteger(3)

creates 4 integer variables which can be described with the expressions MyInteger(1), MyInteger(2), MyInteger(3), and MyInteger(4).

Template:Documentation/Note

Multi-Dimensional Data Fields

In addition to single dimensional data fields, Apache OpenOffice Basic also supports work with multi-dimensional data fields. The corresponding dimensions are separated from one another by commas. The example

Dim MyIntArray(5, 5)

defines an integer array with two dimensions, each with 6 indexes (can be addressed through the indexes 0 to 5). The entire array can record a total of 6 x 6 = 36 integer values.

You can define hundreds of dimensions in Apache OpenOffice Basic Arrays; however, the amount of available memory limits the number of dimensions you can have.

Dynamic Changes in the Dimensions of Data Fields

The previous examples are based on data fields of a specified dimension. You can also define arrays in which the dimension of the data fields dynamically changes. For example, you can define an array to contain all of the words in a text that begin with the letter A. As the number of these words is initially unknown, you need to be able to subsequently change the field limits. To do this in Apache OpenOffice Basic, use the following call:

ReDim MyArray(10)

Template:Documentation/Note

The following example changes the dimension of the initial array so that it can record 11 or 21 values:

Dim MyArray(4) As Integer ' Declaration with five elements 
' ...
ReDim MyArray(10) As Integer ' Increase to 11 elements
' ... 
ReDim MyArray(20) As Integer ' Increase to 21 elements

When you reset the dimensions of an array, you can use any of the options outlined in the previous sections. This includes declaring multi-dimensional data fields and specifying explicit start and end values. When the dimensions of the data field are changed, all contents are lost. If you want to keep the original values, use the Preserve command:

Dim MyArray(10) As Integer ' Defining the initial 
' dimensions
' ... 
ReDim Preserve MyArray(20) As Integer ' Increase in 
' data field, while
' retaining content

When you use Preserve, ensure that the number of dimensions and the type of variables remain the same.

Template:Documentation/Note

If you use ReDim with Preserve, you must use the same data type as specified in the original data field declaration.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools