Sinclair QL Beginner's Manual page 70

Hide thumbs Also See for QL:
Table of Contents

Advertisement

You can see that the answer is defined by the numbers 6 to 9 which indicate where it is. You can
abstract the answer as shown :
100 jumble$ = "APQOLLIONATSUZ"
110 LET an$ = jumble$(6 TO 9)
120 PRINT an$
REPLACE A STRING SLICE
Now suppose that you wish to change the hidden animal into a bull. You can write two extra lines:
130 LET jumble$(6 TO 9) = "BULL"
140 PRINT jumble$
The output from the whole five-line program is:
LION
APQOLBULLATSUZ
All string variables are initially empty, they have length zero. If you attempt to copy a string into a
string-slice which has insufficient length then the assignment may not be recognised by SuperBASIC.
If you wish to copy a string into a string-slice then it is best to ensure the destination string is long
enough by padding it first with spaces.
100 LET subject$ = "ENGLISH MATHS COMPUTING"
110 LET student$ = ""
120 LET student$(9 TO 13) = subject$(9 TO 13)
We say that "BULL" is a slice of the string "APQOLBULLATSUZ". The defining phrase:
(6 TO 9)
is called a slicer. It has other uses. Notice how the same notation may be used on both sides
of the LET statement. If you want to refer to a single character it would be clumsy to write:
jumble$(6 TO 6)
just to pick out the "B" (possibly as a clue) so you can write instead:
jumble$(6)
to refer to a single character
COERCION
Suppose you have a variable, mark$ holding a record of examination marks. The slice
giving the history mark may be extracted and scaled up, perhaps because the history
teacher has been too strict in the marking. The following lines will extract the history
mark:
100 LET mark$ = "625671"
110 LET hist$ = mark$(3 TO 4)

Advertisement

Table of Contents
loading

Table of Contents