Sinclair QL Beginner's Manual page 71

Hide thumbs Also See for QL:
Table of Contents

Advertisement

The problem now is that the value "56" of the variable, hist$ is a string of characters not numeric data.
If you want to scale it up by multiplying by say 1.125, the value of hist$ must be converted to numeric
data first, SuperBASIC will do this conversion automatically when we type:
120 LET num = 1 .125 * hist$
Line 120 converts the string "56" to the number 56 and multiplies it by 1.125 giving 63.
Now we should replace the old mark by the new mark but now the new mark is still the number 63
and before it can be inserted back into the original string it must be converted back to the string '63'.
Again SuperBASIC will convert the number automatically when we type:
130 LET mark$(3 TO 4) = num
140 PRINT mark$
The output from the whole program is:
626371
which shows the history mark increased to 63.
Strictly speaking it is illegal to mix data types in a LET statement. It would be silly to write:
LET num = "LION"
and you would get an error message if you tried, but if you write:
LET num = "65"
the system will conclude that you want the number 65 to become the value of num and do
that. The complete program is:
100 LET mark$ = "625671"
110 LET hist$ = mark$(3 TO 4)
120 LET num = 1.125 * hist$
130 LET mark$(3 TO 4) = num
140 PRINT mark$
Again the output is the same!
In line 120 a string value was converted into numeric form so that it could be multiplied; In line 130 a
number was converted into string form. This converting of data types is known as type coercion.
You can write the program more economically if you understand both string-slicing and coercion now:
100 LET mark$ = "625671"
110 LET mark$(3 TO 4) = 1 .125 * mark$(3 TO 4)
120 PRINT mark$
If you have worked with other BASICs you will appreciate the simplicity and power of string-slicing
and coercion.
SEARCHING A STRING
You can search a string for a given substring. The following program displays a jumble of letters and
invites you to spot the animal.
100 REM Animal Spotting
110 LET jumble$ = "SYNDICATE"
120 PRINT jumble$

Advertisement

Table of Contents
loading

Table of Contents