-
Notifications
You must be signed in to change notification settings - Fork 0
Arrays
Kristian Virtanen edited this page Jan 11, 2026
·
3 revisions
Arrays store collections of values. BazzBasic arrays are fully dynamic and support numeric, string, or mixed indexing.
- Arrays store values that can change during program execution.
- All array names must end with
$.
- Arrays must be declared with
DIMbefore they can be used - BazzBasic arrays are not typed, but work the same way as in JavaScript, for example.
- An array needs the suffix '$'.
DIM scores$
DIM names$
DIM matrix$Multiple declarations:
DIM a$, b$, c$Use numbers as indices (0-based):
DIM scores$
scores$(0) = 95
scores$(1) = 87
scores$(2) = 92
PRINT scores$(0) ' Output: 95Use strings as keys:
DIM player$
player$("name") = "Alice"
player$("score") = 1500
player$("level") = 3
PRINT player$("name") ' Output: AliceUse multiple indices separated by commas:
DIM matrix$
matrix$(0, 0) = "A1"
matrix$(0, 1) = "A2"
matrix$(1, 0) = "B1"
matrix$(1, 1) = "B2"
PRINT matrix$(1, 0) ' Output: B1Combine numeric and string indices:
DIM data$
data$(1, "header") = "Name"
data$(1, "value") = "Alice"
data$(2, "header") = "Age"
data$(2, "value") = 30
PRINT data$(1, "value") ' Output: AliceReturns the number of elements in an array:
DIM items$
items$(0) = "apple"
items$(1) = "banana"
items$(2) = "cherry"
PRINT LEN(items$()) ' Output: 3Note the empty parentheses () after the array name.
Returns 1 if the key exists, 0 otherwise:
DIM config$
config$("debug") = 1
IF HASKEY(config$("debug")) THEN
PRINT "Debug mode is set"
END IF
IF HASKEY(config$("verbose")) = 0 THEN
PRINT "Verbose not set"
END IFRemoves an element from the array:
DIM cache$
cache$("temp") = "value"
PRINT HASKEY(cache$("temp")) ' Output: 1
DELKEY cache$("temp")
PRINT HASKEY(cache$("temp")) ' Output: 0Removes the entire array and all its elements:
DIM arr$
arr$("name") = "Test"
arr$(0) = "Zero"
PRINT LEN(arr$()) ' Output: 2
DELARRAY arr$
' Array no longer exists, can be re-declared
DIM arr$
PRINT LEN(arr$()) ' Output: 0Arrays cannot be passed directly to functions. Instead, pass individual elements as values:
DIM a$
a$("name") = "Foo"
a$("age") = 19
a$(1) = 1
DEF FN func$(a$, b$, c$)
PRINT a$
PRINT b$
PRINT c$
RETURN 0
END DEF
LET b$ = FN func$(a$("name"), a$("age"), a$(1))
' Output:
' Foo
' 19
' 1DIM fruits$
LET count$ = 0
fruits$(count$) = "Apple"
count$ = count$ + 1
fruits$(count$) = "Banana"
count$ = count$ + 1
fruits$(count$) = "Cherry"
count$ = count$ + 1
FOR i$ = 0 TO count$ - 1
PRINT fruits$(i$)
NEXTDIM translations$
translations$("hello") = "hei"
translations$("goodbye") = "nakemiin"
translations$("thanks") = "kiitos"
INPUT "English word: ", word$
IF HASKEY(translations$(word$)) THEN
PRINT "Finnish: "; translations$(word$)
ELSE
PRINT "Translation not found"
END IFDIM grid$
' Initialize 3x3 grid
FOR row$ = 0 TO 2
FOR col$ = 0 TO 2
grid$(row$, col$) = "."
NEXT
NEXT
' Place some markers
grid$(0, 0) = "X"
grid$(1, 1) = "O"
grid$(2, 2) = "X"
' Print grid
FOR row$ = 0 TO 2
FOR col$ = 0 TO 2
PRINT grid$(row$, col$); " ";
NEXT
PRINT ""
NEXTOutput:
X . .
. O .
. . X
scores$(0) = 95
' ERROR: Array not declared, use DIM first: scores$DIM data$
PRINT data$(0)
' ERROR: Array element data$(0) not initializedAlways check with HASKEY or initialize elements before reading.