12.4 Using String Virtual Arrays
Virtual arrays are stored on disk. You create a virtual array
by opening a disk file and then using the DIM # statement
to dimension the array on the open channel. This section de-
scribes only string virtual arrays. See Chapter 14 for more
information about virtual arrays.
Elements of string virtual arrays behave much like dynamic
strings, with the following exceptions:
.
When you create the virtual string array, you specify a
maximum length for the array's elements. The length
of an array element can never exceed this maximum. If
you do not supply a length, the default is 16 characters.
.
A string virtual array element cannot contain trailing
nulls.
When you assign a value to a string virtual array element,
BASIC pads the value with nulls, if necessary, to fit the length
of the virtual array element. However, when you retrieve
the virtual array element, BASIC strips all trailing nulls from
the string. Therefore, when you access an element in a string
virtual array, the string never has trailing nulls.
In the following example, the first two lines dimension a
string virtual array and open a file on channel #1. The third
line assigns a 10-character string to the first element of
this string array, and to the variable A$ . This 10-character
string consists of ``ABCDE'' plus five null characters. The
PRINT statements show that the length of A$ is 10, while the
length of test(1) is only 5 because BASIC strips trailing nulls
from string array elements.
DIM #1%, STRING test(5)
OPEN "TEST" AS FILE #1%, ORGANIZATION VIRTUAL
A$, test(1%) = "ABCDE" + STRING$(5%, 0%)
PRINT "LENGTH OF A$ IS: "; LEN(A$)
PRINT "LENGTH OF TEST(1) IS: "; LEN(test(1%))
END
Output
LENGTH OF A$ IS: 10
LENGTH OF TEST(1) IS: 5
Although the storage for string virtual array elements is
fixed, the length of a string array element can change be-
cause BASIC strips the trailing nulls whenever it retrieves a
value from the array.