Peter Hale, P.O. Box 8763, Boston, MA 02114
String concatenation is a subject that can’t easily be covered in a vacuum. It must include string slicing and coercion as well.
A good starting point on those topics is in the Concepts section of the QL manual. See coercion on page 11, data types and variables on page 13, slicing on page 46 and string arrays/variables on page 51.
What follows here integrates those subjects.
A SuperBASIC (or Archive) string can have up to 32766 characters. It can be stored as a variable if the variable has the suffix $.
Any of the 256 characters in the QL character set can be part of a string, although non-printing characters must be entered as code. Whether entered as code or as a printing character, each character requires only one space in the string.
In SuperBASIC each character in a string is located by its distance from the first character. In the string a$=”abcdedghij” the letter “a” is in first position, the letter “j” is in tenth position. As in other Sinclair BASICS, the letter “a” is found by a$(1) and “j” by a$(10).
Other, less friendly BASICS, make you use MID$, LEFT$ or RIGHT$ functions depending on the character’s position in the string.
SuperBASIC string slicing is elegantly simple. The syntax of string slicing is unchanged whether the character is in first, last or mid position.
Getting more than one consecutive character out of a string is similarly straightforward and simple. The syntax is a$( m TO n) where m is the position of the first character and n is that of the last. Thus:
a$(1 to 3) gives "abc" a$(3 to 6) gives "def" a$(7 to 7) gives "g" a$(8 to ) gives "hij"
When selecting the balance of a string it is unnecessary to specify the position of the last character.
Coercion
A handy feature of SuperBASIC is coercion. This has nothing to do with arm twisting; it means that a number can be represented as a numerical variable or a string variable depending on the kind of variable defined, and not on whether or not there are quotes.
If you set the variable b=12, it will be stored as a number variable. On the other hand, if you define the variable b$=12, it will be stored as a string variable just as if you had written b$=”12″.
You can move back and forth through the magic of coercion; that is, b=b$ changes the string variable b$ into a number variable and vice versa.
The restriction, of course, is that you must deal with numbers. Try letting a=a$ in the above example and you crash. It is also a limitation that numbers are stored accurately only to eight (8) significant digits because it is all done in floating point.
(Floating point – a digression: This means that the number is stored in two bytes as a number raised to the power of 10. The second byte is the power of ten to which the first byte is raised.
Thus, b=123456:b$=b returns b$ as “123456”. Longer numbers are handled differently. c=1234567:c$=c returns c$ as “1.234567E6”; d=12345678:d$=d returns d$ as “1.2345678E7”; e=123456789:e$=e holds e$ as “1.2345678E8”.
Clearly, b$ and b look the same. c$ and c and d$ and d are both accurate, although they look different because the last two characters are the power of ten to which the preceding numbers are raised. e$ and e differ because the ninth digit is truncated.
The moral for using coercion is to stay with numbers under 9 digits; for clarity, stay under 7 digits.)
Coercion tolerates a limited mix of numbers and text, but does not like text.
If f$=”1234A”, coercion allows the instruction f=f$, but returns f=1234. It will not permit coercion of g=”A1234″ since the first character is not a number.
Concatenation
The struggle through both string slicing and coercion helps to understand the full potential of concatenation in the Sinclair QL.
SuperBASIC joins two strings to make a third with the operator & (Concepts page 35).
Whenever SuperBASIC sees the operator &, it knows to treat whatever variables are involved as strings.
Thus x$=a$&b$ or x$=a$&b will each return “abcdefghij123456”.
If the operator is +, SuperBASIC knows that it should to do an arithmetical operation.
Whether you write x=b+c or x=b$+c$ or x$=b$+c$, the answer for either x or x$ will be 123456 + 1234567 or 1358023.
x=a$+b or x$=a$+b will crash since the first character in a$ is non-numeric (‘a’).
Really, that’s all there is to it, although some examples (using the variables in the text above) may help.
1. a$(8 to 9)&a$(4 to 5)="hide"
2. a$(2)&a$(9)&a$(7)="big"
3. b$+b$=246912
4. b$&b$="123456123456"
5. a$(2)&b="b23456"
6. b$(4)&b$(2)="42" but b$(2)&b$(4)="24"
7. b$(4)+b$(2)=6 or b$(2)+b$(4)=6
8. b$(2 to 3)+b$(4 to 5)=68
9. b$(2 to 3)&b$(4 to 5)="2345"
10. y$=a$(1 to len(a$)-4): z$=b$(4 to 5)
y$&z$="abcdef45"
11. if x$=a$&b$="abcdefghij123456" then
x$(4)&x$(9)&x$(7)&" "&x$(12)&" "&
x$(8 to 9)&x$(4 to 5)= ?
Just don’t try b(4)+b(5); number slicing doesn’t work. Coerce first, then be gentle, applies to SuperBASIC, but not to life in general.