Array

Functions that work on arrays. Dim and ReDim themselves are part of the language: see Arrays.

Arrays are passed to these functions with empty brackets: ArraySize(scores()).

ArraySize 🟡

Result = ArraySize(Array() [, Dimension])

The last valid index of an array: the number given to Dim. An array created with Dim a(9) gives 9, and has 10 elements.

Parameter Meaning
Array() the array
Dimension which dimension, for multi-dimensional arrays. Ignored for now: the first dimension is always used
OpenConsole()
Dim scores.i(9)
PrintN(Str(ArraySize(scores())))
ReDim scores(19)
PrintN(Str(ArraySize(scores())))

Output:

9
19

Notes. PureBasic returns −1 for an array that hasn't been created with Dim yet. x doesn't check this yet.

CopyArray ✅

Result = CopyArray(SourceArray(), DestinationArray())

Makes the destination array a copy of the source: same size, same elements. Strings are shared with the copy, and later changes to one array don't affect the other. The destination array must have been declared with Dim (any size).

FreeArray ✅

FreeArray(Array())

Frees an array's elements now, instead of when the program or procedure ends. Use Dim to create it again.

Dim src.s(2)
src(0) = "x" : src(1) = "y" : src(2) = "z"
Dim dst.s(0)
CopyArray(src(), dst())
src(1) = "changed"

OpenConsole()
PrintN(dst(0) + dst(1) + dst(2) + " " + Str(ArraySize(dst())))

Output:

xyz 2

Bit arrays — x additions

These work on bit arrays (Dim name.bit(n), see Arrays). They go a quad at a time, 64 bits per step. With two dimensions they count the bits row after row, so an index here is i * (Size2 + 1) + j.

Count = BitCount(Array())
Index = FirstBit(Array() [, From])
Index = FirstClearBit(Array() [, From])
FillBits(Array(), Value)
AndBits(Array(), Other())
OrBits(Array(), Other())
XorBits(Array(), Other())
NotBits(Array())
  • BitCount returns how many bits are 1.
  • FirstBit returns the index of the first 1 at From (0 by default) or after it, or -1 if there is none. FirstClearBit does the same for 0.
  • FillBits sets every bit to 0, or to 1 if Value isn't 0. NotBits flips every bit.
  • AndBits, OrBits and XorBits change Array(), combining it with Other() bit by bit. If Other() is shorter, its missing bits count as 0.

These functions are not atomic. In thread mode, don't change the array from other threads while one runs.

OpenConsole()
Dim a.bit(9)
Dim b.bit(9)
a(1) = 1 : a(2) = 1 : a(3) = 1
b(2) = 1 : b(3) = 1 : b(4) = 1
AndBits(a(), b())                     ; a: 2 and 3
PrintN(Str(BitCount(a())) + " " + Str(FirstBit(a())) + " " + Str(FirstBit(a(), 3)) + " " + Str(FirstBit(a(), 4)))
FillBits(b(), 1)
PrintN(Str(BitCount(b())) + " " + Str(FirstClearBit(b())))
NotBits(a())
PrintN(Str(BitCount(a())) + " " + Str(FirstClearBit(a())))

Output:

2 2 3 -1
10 -1
8 2

Not yet available 🔜

SortArray, and ArraySize with a dimension. The sorting functions don't take bit arrays.