Arrays

Dim

Dim name.type(last) creates an array with elements 0 to last, so it has last + 1 elements. Elements start at 0.

OpenConsole()
Dim squares.i(5)
For i = 0 To 5
  squares(i) = i * i
Next
PrintN(Str(squares(5)) + ", last index " + Str(ArraySize(squares())))

Output:

25, last index 5

[ ] works for indexing too: squares[i].

Running Dim again on the same array creates it afresh, with every element back to 0.

Two dimensions

OpenConsole()
Dim grid.l(2, 3)          ; 3 rows of 4
grid(2, 3) = 9
grid(1, 0) = 4
PrintN(Str(grid(2, 3) + grid(1, 0)))

Output:

13

🟡 Stage 0 supports one and two dimensions.

ReDim

ReDim changes the size of the last dimension and keeps the existing elements. New elements are 0.

OpenConsole()
Dim items.i(2)
items(2) = 5
ReDim items(10)
items(10) = 1
PrintN(Str(items(2) + items(10)) + " " + Str(ArraySize(items())))

Output:

6 10

Arrays as parameters

Arrays are passed by reference. The parameter gives the element type and the number of dimensions:

Procedure.i Total(Array v.i(1))
  Protected sum
  For i = 0 To ArraySize(v())
    sum + v(i)
  Next
  ProcedureReturn sum
EndProcedure

OpenConsole()
Dim nums.i(4)
For i = 0 To 4
  nums(i) = i + 1
Next
PrintN(Str(Total(nums())))

Output:

15

Arrays of strings

Dim names.s(2)
names(0) = "Ada"
names(1) = "Grace"
names(2) = names(0) + " & " + names(1)

OpenConsole()
PrintN(names(2))

Output:

Ada & Grace

Strings in an array are released when the array is freed, re-created with Dim, or shrunk with ReDim.

Local arrays

An array created with Dim inside a procedure belongs to that call, and is freed when the procedure returns.

Address of the elements

@name() is the address of the first element. Elements are stored one after another with no gaps.

Static arrays

A fixed-size array inside a structure is written field.type[N], and has exactly N elements, 0 to N − 1. Note the difference from Dim. See Structures.

🔜 C-style arrays as variables: int a[10].

Names

An array is always written with (), so its name doesn't clash with a variable or a constant: count and count() are two different things. The same goes for lists and maps.

OpenConsole()
Dim count.i(3)
count = 40                    ; a variable, separate from the array
For i = 0 To 3
  count(i) = i * 10
Next
PrintN(Str(count) + " " + Str(count(3)))

Output:

40 30

Bit arrays (x addition)

PureBasic has no bit arrays. People fake them with PeekQ, PokeQ and shifts. In x, .bit is an element type for arrays:

Dim name.bit(Size [, Size2])

Each element is one bit, packed 64 to a quad, so a million flags take 125 KB. An element reads as 0 or 1. Storing a value that isn't 0 sets it to 1.

Dim, ReDim, ArraySize, CopyArray, FreeArray and Array parameters all work as they do for other arrays. Elements can be changed with =, an operator (seen(i) ! 1 toggles one), ++, -- and Atomic. A bit has no address, so @name(i) and Swap don't work on one. The functions in the Array library work on whole bit arrays.

OpenConsole()
n = 50
Dim composite.bit(n)            ; the sieve of Eratosthenes
For i = 2 To n
  If composite(i) = 0
    j = i * i
    While j <= n
      composite(j) = 1
      j + i
    Wend
  EndIf
Next
primes$ = ""
For i = 2 To n
  If Not composite(i)
    primes$ + Str(i) + " "
  EndIf
Next
PrintN(primes$)
Dim flags.bit(9)
flags(3) = 1
flags(3) ! 1                    ; toggle
flags(5)++
PrintN(Str(flags(3)) + Str(flags(5)))

Output:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
01

In thread mode each change of a bit is atomic. Threads changing neighbouring bits of the same quad, such as in a Parallel For, don't lose each other's changes. Reading an element, and setting it to a constant, are compiled inline. Anything else calls the runtime.

.bit can't be used for a variable, a structure field, a list or a map. Fields that are a few bits wide (bitfields) are on the roadmap.

Not yet available 🔜

  • More than two dimensions.
  • ArraySize with a dimension number.