Dim
DescriptionDim name.<type>(<expression>, [<expression>], ...)
Dim is used to create new arrays (the initial value of each element will be zero). An array in PureBasic can be of any types, including structured, and user defined types. Once an array is defined it can be resized with ReDim. Arrays are dynamically allocated which means a variable or an expression can be used to size them. To view all commands used to manage arrays, see the Array library.
When you define a new array, please note that it will have one more element than you used as parameter, because the numbering of the elements in PureBasic (like in other BASIC's) starts at element 0. For example when you define Dim(10) the array will have 11 elements, elements 0 to 10. This behavior is different for static arrays in structures.
The new arrays are always locals, which means Global or Shared commands have to be used if an array declared in the main source need to be used in procedures. It is also possible to pass an array as parameter to a procedure - by using the keyword Array. It will be passed "by reference" (which means, that the array will not be copied, instead the functions in the procedure will manipulate the original array).
To delete the content of an array and release its used memory during program flow, call FreeArray().
If Dim is used on an existing array, it will reset its contents to zero.
For fast swapping of array contents the Swap keyword is available.
Note: Array bound checking is only done when the runtime Debugger is enabled.
Example
Dim MyArray(41) MyArray(0) = 1 MyArray(1) = 2
Example: Multidimensional array
Dim MultiArray.b(NbColumns, NbLines) MultiArray(10, 20) = 10 MultiArray(20, 30) = 20
Example: Array as procedure parameter
Procedure fill(Array Numbers(1), Length) ; the 1 stays for the number of dimensions in the array For i = 0 To Length Numbers(i) = i Next EndProcedure Dim Numbers(10) fill(Numbers(), 10) ; the array Numbers() will be passed as parameter here Debug Numbers(5) Debug Numbers(10)
DescriptionReDim name.<type>(<expression>, [<expression>], ...)
ReDim is used to 'resize' an already declared array while preserving its content. The new size can be larger or smaller, but the number of dimension of the array can not be changed.
If ReDim is used with a multi-dimension array, only its last dimension can be changed.
Example
Dim MyArray.l(1) ; We have 2 elements MyArray(0) = 1 MyArray(1) = 2 ReDim MyArray(4) ; Now we want 5 elements MyArray(2) = 3 For k = 0 To 2 Debug MyArray(k) Next