Commodore 2001-8 User Manual page 42

Table of Contents

Advertisement

Chapter 6.
ADVANCED PROGRAMMING TECHNIQUES
We have been describing numeric functions primarily, but almost any useful program also has to deal
with alphanumeric data. BASIC has a set of functions to deal with these data. Also, all alphanumeric data
may be expressed as a continuous connection of characters which is viewed by BASIC as the value of a
single variable.
PET BASIC, has a $ notation which is used to express variables which are strings of alphanumeric data.
All of the rules which apply to normal variables apply to the string variable.
Following the naming conventions, we can create a variable A$ not equal to A% and not equal to A.
Type A$
=
"NOW IS THE TIME" and PRINT A$to show the value of the string. This technique can define a
string of a length up to about 70 characters, depending on the number of characters of the line number--
ali that can be entered on a line. However, the limitation on the number of characters that can be stored
in a string is 255. You can build strings larger than can be entered. The accumulation of characters from
an 1/0 device and the construction of data is accomplished by the concatenation of strings. The operator
that is used is
+
We can modify the expression A$ which we have been developing by typing A$ = A$
+"
FOR ALL". Print
A$ and you can see that the literal we typed in had a space at the beginning. Unlike numbers Which are
formatted by BASIC, the value of the literal is taken literally. A string can contain aJl combinations of bits
including those that form control characters such as cursor down, and carriage return. This will be
illustrated soon.
BASIC allows string expressions up to 255 characters long. These can be output to the screen or to any
output device which accepts more than 79 characters. Input, however, is usually restricted to 79
characters because of the size of the input buffer. This problem can be handled by breaking strings into
substrings before they are input or by using GET to input each character individually. The substrings or
individual characters can then be recombined into the original string by concatenation.
COMPARISON OF STRINGS
The ASCII table is defined in Figure 2.6. It contains the order in which characters within the PET are
represented when two strings are compared. Characters within a set of strings are compared starting at
the leftmost character to the end of the field specified.
Using the ASCII table, we can compare a string containing an "A" to one containing a "B" in the same
position. The result is that the second string is greater than the first.
A string containing a blank is less than a "1", which is less than an "A", which is less than a "8". The
string "A" is less than the string "ABC" or any string containing "A" as the first character. All characters
are compared in sequence with the first unequal character defining the relationship between the strings.
Thus the same relational functions may be used for both strings and numbers.
< >
for unequal
=
for equal
<
for less than
>
for greater than
Immediately the string comparison feature can be applied to help you construct ordered lists such as a
check file or a telephone directory. Comparisons can also be used to search ordered lists such as a file or
a telephone directory.
38

Advertisement

Table of Contents
loading

This manual is also suitable for:

Pet 2001-8

Table of Contents