Commodore 2001-8 User Manual page 44

Table of Contents

Advertisement

ASC
ASC turns a character into an ASCII code number which may be used in numerical calculations. The
parameter is a string.
?ASC("A")
65
If the string consists of multiple characters, then this function will return the code tor the first character
of the string.
?ASC("123")
49
The ASCII code for "1" is 49.
SEGMENT OF STRINGS
In many cases
it
is desirable to access just part of a string in developing an ordered list. Consider the
problem where in response to an INPUT, a person's name is typed in. It might consist of their first name,
middle intiHal, and last name. It is important that for sorting, however, that not all Johns be together, but
that the list be ordered by last name.
In order to be able to separate parts of strings and use them in expressions, PET BASIC provides three
functions. Most of your programming with strings will consist of using one of these three functions to
analyze pieces of a constructed string. We will present the use of the functions and define all three at
once as they are essentially the same function. Three combinations are provided mainly for programming
convenience.
LEFT$, RIGHT$, and MID$
The function specified as LEFT$(string variable,
t)
gives the leftmost "I" characters of the string
specified. If I
is negative, or zero, or greater than 255, then an ILLEGAL QUANTITY ERROR is printed. RIGHT$(STRING
VARIABLE, I)gives the rightmost "I" characters of the string expression. When "I" is less than, or equal
to zero,
or greater than 255, an ILLEGAL QUANTITY ERROR is printed.
There are two expressions for MID$. The first most general one is MID$(STRING VARIABLE, I,
J).
This
expression gives "J" characters from the string starting with the "r"th character. If "I" is greater than the
length of the string, then this will give a null string. If either "I" or
"J"
negative, or greater than 255, an
ILLEGAL QUANTITY ERROR is printed. For "J" greater than the number of characters left in the string, all
the characters from "I" to the end of the string are returned.
The second expression is MID$(STRING VARIABLE,
I)
which is the same as specifying a "J" greater than
the length of the string. All the characters starting in the "I" position until the end of the string are
returned. If "I" is greater than the length of the string, then a null string is returned and if "I" is negative,
zero, or greater than 255, and ILLEGAL QUANTITY ERROR is printed.
All of these variables combined will define a new function which allows us to take either the left number
of characters, right number of characters, or a given number of characters starting at a given position of
the string.
To find the last name from our previous example, we can analyze characters starting from the rightmost
character of the string until the first blank is encountered. To implement this program we need one more
function.
LENGTH OF A STRING
The LEN function gives an exact count of the number of characters contained in a string. Non-printing
40

Advertisement

Table of Contents
loading

This manual is also suitable for:

Pet 2001-8

Table of Contents