BeSly Software Solutions About yab Demos About us Admin


dim -- create an array prior to its first use

dim array(d,e) 
dim array$(d,e) 

The dim-command prepares one or more arrays (of either strings or numbers) for later use. This command can also be used to enlarges an existing array.

When an array is created with the dim-statement, memory is allocated and all elements are initialized with either 0 (for numerical arrays) or "" (for string arrays).
If the array already existed, and the dim-statement specifies a larger size than the current size, the array is enlarged and any old content is preserved.

Note, that dim cannot be used to shrink an array: If you specify a size, that is smaller than the current size, the dim-command does nothing.

Finally: To create an array, that is only known within a single subroutine, you should use the command local, which creates local variables as well as local arrays.

dim a(5,5) 
for x=1 to 5
	for y=1 to 5 
next x 
dim a(7,7) 

sub printmatrix(ar()) 
	local x,y,p,q 
	for q=1 to y 
		for p=1 to y 
			print ar(p,q),"\t" 
		next p 
	next q 
end sub 

This example creates a 2-dimenional array (i.e. a matrix) with the dim-statement and fills it with random numbers. The second dim-statement enlarges the array, all new elements are filled with 0.

The subroutine printmatrix just does, what its name says.

Related: arraysize, arraydim, local