Scripting 3.4. String variables

Just as you can store numeric variables, you can store string variables, which contain text instead of numbers. Here is an example:

word1$ = "Hello"
word2$ = "world"
sentence$ = word1$ + " " + word2$
writeInfoLine: "The whole sentence is: ", sentence$

Yes, this is another way to get the sentence Hello world into the Info window. It's a more linguistically valid way to do it, and here is how it works:

1. In line 1, the value "Hello", which is a text (as we can see by its use of quotes), is stored into the variable word1$, which is a string variable (as we can see because its name ends in a dollar sign).
2. In line 2, the text value "world" is stored into the string variable word2$.
3. In line 3, we have the formula word1$ + " " + word2$, which contains two variables, namely word1$ and word2$.
4. The values of the two variables are "Hello" and "world", respectively, so what the formula actually says is "Hello" + " " + "world".
5. The pluses in the formula mean "concatenate", so we concatenate the three strings "Hello", " ", and "world", giving the longer string "Hello world".
6. Still in line 3, the string value "Hello world" is assigned to the string variable sentence$.
7. Line 4 reports in the Info window: The whole sentence is: Hello world

Links to this page


© ppgb, April 11, 2013