BASIC Variable and Arrays - fvdhoef/aquarius-plus GitHub Wiki
Variable and Arrays
Floating-point and String Variables
Variables are names that represent data values used in your BASIC statements. The value represented by a variable can be assigned by setting it equal to a constant, or it can be the result of calculations in the program. Variable data, like constants, can be floating-point numbers, or strings.
If you refer to a variable name in a program before a value has been assigned, the BASIC Interpreter will automatically create the variable with a value of 0
if it's a floating-point number, or '""' if it's string.
Variable Names
Variable names can be any length but only the first two characters are considered significant in Aquarius BASIC. This means that all names used for variables must NOT have the same first two characters.
Variable names may NOT be the same as BASIC keywords and they may NOT contain keywords in the middle of variable names. Keywords include all BASIC commands, statements, function names and logical operator names. A Syntax error will result if you accidentally use a keyword in the middle of a variable name.
The characters used to form variable names are the alphabet and the numbers 0-9. The first character of the name must be a letter.
A dollar sign $
at the end of a variable name declares a string variable, otherwise the variable type is floating-point.
Examples:
- Legal variable names
A$="GROSS SALES"
MTH$="JAN"+A$
FP=12.5
SUM=FP*CNT%
- Illegal variable names
FORT
BAND
plusBASIC enhancement (v0.17)
A one or two character variable name may be followed with a tilde ~
followed by a series of letters, digits or tildes (and optional $
).
This series can include keywords, so space is required between a numeric tilde variable and any following keyword, such as AND
, OR
, or XOR
.
Examples:
- Legal tilde variable names
A~TITLE$="GROSS SALES"
M1~MONTH$="JAN"+A$
FP~FLOAT=12.5
S~SUM=FP*CNT%
T~TABLE(X)=D
- Illegal tilde variable names
OR~OLDRANGE
T(X)~TABLE
Floating-point and String Arrays
An array is a table (or list) of associated data items referred to by a single variable name. In other words, an array is a sequence of related variables.
A table of numbers can be seen as an array, with the individual numbers within the table being elements of the array.
Arrays are a useful shorthand way of describing a large number of related variables. For example. table with 10 rows with 20 numbers in each row would total 200 different numbers. Without a single array name to call on you would have to assign a unique name to each value in the table. But because you can use arrays you only need one name for the array and all the elements in the array are identified by their individual locations within the array.
Array names can be floating-point or string data types and all elements in the array have the same data type as the array name. Arrays can have a single dimension (as in a simple list) or they can have multiple dimensions (such as a grid marked in rows and columns). Each element of an array is uniquely identified and referred to by a subscript (or index variable) following the array name enclosed within parentheses.
The maximum number of dimensions an array can have in theory is 255 and the number of elements in each dimension is limited to 32767, but for practical purposes array sizes are limited by the memory space available to hold their data and/or the 72 character length. If an array has only one dimension and its subscript value will never exceed 11 items (indexes 0 thru 10), then the array will be created by the Interpreter and filled with zeros (or nulls if string type) the first time any element of the array is referred to, otherwise the BASIC DIM statement must be used to define the shape and size of the array.
Advanced:
The amount of memory required to store an array can be determined as follows:
- 2 bytes for the array name
- 2 bytes for the length of the array data in bytes
- 1 byte for the number of dimensions
- 2 bytes for each dimension of the array
- 4 bytes per element
Subscripts can be integer constants, variables, or an arithmetic expression which gives an integer result. Separate subscripts, with commas between them, are required for each dimension of an array. Subscripts can have values from zero up to the number of elements in the respective dimensions of the array. Values outside that range will cause the BASIC error message Bad subscript. Some examples of array names, value assignments and data types are:
Examples:
A$(0)="GROSS SALES"
MTH$(K)="JAN"
FP(12*K)=24.8
SUM(CNT(1))=FP^K%
A$(5)="Text"
Sets the 5th element in the 1 dimensional string array
A
to the the stringTEXT
.)
B(5,6)=99
Sets the element in row position 5 and column position 6 in the 2 dimensional array
B
equal to 99)
C(1,2,3)=N
Sets the element in row position 1, column position 2, and depth position 3 in the 3 dimensional array
C
to the value of variableN
).