Hello, and welcome to the sixth part of the Runtime Basic Tutorial
                     
series.
                     
                     
                     
                    FIRST STEPS IN BASIC
                     
                     
                     
     By now you should have had plenty of practice typing in the short
                     
listing given in the previous articles. If you have, and have managed to
                     
follow the series so far, you've got what it takes to be a programmer!
                     
     On the other hand, it's quite possible that all this talk to variables,
                     
real and integer numbers and rounding up and down is making your head
                     
spin. Well, don;t worry! Fortunately for you, programming in BASIC
                     
doesn't always involve complex calculations and maths.
                     
                     
                     
     In the last two articles, we looked at the two types of BASIC
                     
variable that deal with numbers. In this article, we're going to introduce a
                     
third type called a "STRING".  A string variable is used to store a
                     
sequence - or string -  of characters, which can be letter, numbers or any
                     
other character from the ST character set. A string can always be told
                     
apart from another type of variable by the dollar sign ($) after the name.
                     
Thus both A$ and name$ are acceptable as string variables. (Remember
                     
that the rules we discussed for naming variables still apply. Basic only
                     
looks at the first eight letters, and each variable must have a unique name
                     
to avoid confusion).
                     
                     
                     
     Unlike the numeric variables we have already look at, a string
                     
variable cannot have mathematical operations performed on it. The only
                     
thing that can be done to two strings is to add them together. Joining tow
                     
strings together in this way is known as concentration. Attempting to do
                     
something like a division is meaningless, and causes a BASIC error. For
                     
example:
                     
                     
                     
     10     A$ = "APPLES"
                     
     20     B$ = "PEARS"
                     
     30     PRINT A$ / B$
                     
                     
                     
     To us, dividing APPLES by PEARS might seem to be perfectly
                     
reasonable, but to BASIC it is an impossible operation. Remember that
                     
BASIC can only work with numbers and doesn't understand APPLES and
                     
PEARS as anything other than a string of letters.
                     
                     
                     
     With this limitation, it might seem at first that strings a practically
                     
useless. However, strings are one of the most useful features of BASIC,
                     
as we shall shortly see. Here's our first sample program for this month:
                     
                     
                     
PROGRAM ONE
                     
                     
                     
     10      INPUT "What is your Christian name:";a$
                     
     20      INPUT "What is your surname:";b$
                     
     30      PRINT "Hello ";a$;"";b$
                     
                     
                     
     The program will respond:
                     
                     
                     
     What is your Christian name:?PETER
                     
     What is your surname:?PHILLIPS
                     
     Hello PETER PHILLIPS
                     
                     
                     
     Remember that the INPUT statement automatically adds a
                     
questions mark to the end of the prompt, so you don't need to worry about
                     
putting one in. It might also be useful to leave a couple of spaces after the
                     
colons (:) to make the program look better.
                     
                     
                     
     The second point with this program is the variable names.
                     
Remember that you should always to to give variables a meaningful
                     
name that helps you keep track of them.
                     
                     
                     
     In the following program, we can see how string concentration
                     
works. Also notice that the meanings of the variable names are now much
                     
clearer.
                     
                     
                     
PROGRAM TWO
                     
                     
                     
     10      INPUT "What is your Christian name:";first$
                     
     20      INPUT "What is your surname:";last$
                     
     30      hello$ = "Hello " + first$ + "" + last$
                     
     40      PRINT hello$
                     
                     
                     
     Notice how the semi-colons (;) in the PRINT statement of
                     
PROGRAM ONE have been replace by additions (+) in the string
                     
concentration in line 30 of the new program.
                     
                     
                     
     Strings are always added from the left to right so if line 30 read:
                     
                     
                     
     30     hello$ = "Hello " + last$ + "" + first$
                     
                     
                     
     then the result would be printed as:
                     
                     
                     
     Hello PHILLIPS PETER
                     
                     
                     
     Program Two is also a longer way of achieving the same result.
                     
However, if you want to use hello$ again to address the user by name
                     
several times within the same program, then this is the best way to do it.
                     
techniques like this are often used in games to help address the player in a
                     
friendly manner.
                     
                     
                     
CUTS BOTH WAYS
                     
                     
                     
     Characters, no matter what sort they may be, words, sentences, or
                     
whatever can be manipulated provided they are held as a string. If you
                     
think of a text string like a real piece of string, you won't go far wrong.
                     
We've already seen how to tie two pieces of string together, now how
                     
about cutting them up?
                     
                     
                     
     The table below gives a list of all the operations that can be used to
                     
cut up strings, as well as some others that can be useful. The three most
                     
commonly used operations are LEFT$, RIGHT$ and MED$.
                     
                     
                     
TABLE ONE
                     
                     
                     
     FUNCTION     OPERATION
                     
                     
                     
     LEFT$        Get left part of string
                     
     MED$         Get middle part of string
                     
     RIGHT$       Get right part of string
                     
     LEN          Find the length of the string
                     
     INSTR        IN STRing. Search for one string inside another.
                     
     VAL          Returns the numeric value of the string
                     
     STR$         Convert a numeric value to a string
                     
                     
                     
     To see how some of these operations work, type in the following
                     
program and run it:
                     
                     
                     
PROGRAM THREE
                     
                     
                     
     10     a$ = "Left" + "Middle" + "Right"
                     
     20     PRINT "Left word = ";LEFT$ (a$,4)
                     
     30     PRINT "Right word = ";RIGHT$ (A$,5)
                     
     40     PRINT "Middle word = ";MED$ (a$,5,6)
                     
                     
                     
     This program will return with these answers:
                     
                     
                     
     Left word = Left
                     
     Right word = Right
                     
     Middle word = Middle
                     
                     
                     
     If you try Program Three for yourself, you'll see what the answers
                     
are, but not really understand why the program is returning these results.
                     
It is often necessary to go through a program line by line to understand
                     
what is happening. Lets try it with this one.
                     
                     
                     
                     
                     
     LINE 10     a$ = "Left" + "Middle" + "Right"
                     
                     
                     
     This is a string concentration operation. It creates a string called a$
                     
that contains the letters "LeftMiddleRight". Notice that there are no
                     
spaces between the three words.
                     
                     
                     
                     
                     
     LINE 20     PRINT "Left word = ";LEFT$ (a$,4)
                     
                     
                     
     We already know what a PRINT statement does and why, so its
                     
what's after the semi-colon that matters. This part of the statement
                     
introduce the first of the new string functions, LEFT$.
                     
                     
                     
     Notice that LEFT$ has been given two parameters inside the
                     
brackets, separated by a comma (,). The syntax for the command is:
                     
                     
                     
     x$ = LEFT$ (y$,X)
                     
                     
                     
     where x$ is the character to the leftmost of y$ as denoted by the
                     
value of X. So in our example, what left string does is this:
                     
                     
                     
     Take the four characters (4 = value of X) to the leftmost of string
                     
a$ (a$ is "LeftMiddleRight") and print them to the screen.
                     
                     
                     
     This is translated as the word "Left" as the letters L, E, F, T are the
                     
leftmost four characters of "LeftMiddleRight".
                     
                     
                     
                     
                     
     LINE 30      PRINT "Right word = ";RIGHT$ (A$,5)
                     
                     
                     
     RIGHT string has exactly the same syntax as LEFT$, except that it
                     
counts from the right hand end of the string. In our example it takes the
                     
five letters to the farthest right of the string which are R, I, G, H, and T
                     
and prints them.
                     
                     
                     
                     
                     
     LINE 40     PRINT "Middle word = ";MED$ (a$,5,6)
                     
                     
                     
     MED$ has a slightly different syntax from the other two. It
                     
introduces a third parameter. The complete syntax is:
                     
                     
                     
     x$ = MED$ (y$, start, extent)
                     
                     
                     
     The x$ and y$ parts are exactly the same as for LEFT$ and
                     
RIGHT$ but the START and EXTENT parts are new. They are used like
                     
this:
                     
                     
                     
     START is the number of character into the string to count
                     
     EXTENT is the number of characters to be included
                     
                     
                     
     So our example translates as:
                     
                     
                     
     Count five (start) characters into the string LeftMiddleRight (a$)
                     
and then take the next six characters (extent).
                     
                     
                     
     This then finds the letters M, I, D, D, L, and E because M is the
                     
fifth character and MIDDLE is six letters long. (Note that the START
                     
character is included in the number counted by EXTENT).
                     
                     
                     
                     
                     
BEFORE NEXT TIME
                     
                     
                     
     Try and write a program to input two strings, concentrate them and
                     
then output the result. This should be fairly easy if you follow the
                     
examples given.
                     
                     
                     
     Try to write a program to find letters of the alphabet, both forwards
                     
and backwards. What is the ninth letter of the alphabet? What is the sixth
                     
letter from the end? HINT: Set up your a$ to contain the letters of the
                     
alphabet, and an INPUT statement to get the number of the letter
                     
required. Then use LEFT$ and RIGHT$ to find the correct letters. I'll
                     
include a sample of the sort of program you should have written next
                     
time.
                     
                     

Takaisin

(C) Marko, Suomen Atari-sivut / ArkiSTo 2003