[ Team LiB ] |
7.1 Assignment and RetrievalTo assign a value to a variable is to put something into the shoebox. If the variable already has a value, that value is replaced. Assignment is performed with one of two commands: set or copy, described here.
Syntaxset variableName to valueDescriptionAssigns value to variableName. Exampleset x to 5 There is a synonym using the word returning instead of set, with the parameters in reverse order, like this: 5 returning x. But I have never seen this used.
Syntaxcopy value to variableNameDescriptionAssigns value to variableName. Examplecopy 5 to x An abbreviation for copy is put ; an abbreviation for to is into. Thus you could type put 5 into x, although it would still come out as copy 5 to x. This is doubtless to accommodate HyperCard users, who were habituated to this syntax. In these expressions, variableName can optionally be a list of variable names, allowing multiple assignments in one command. The value, too, will then be a list—a list of the values to be assigned. The first item in the value list is assigned to the first item in the variableName list, the second to the second, and so forth. If the value list is longer than the variableName list, the extra values are not assigned to anything; if the value list is shorter than the variableName list, there is a runtime error. This remarkably elegant feature is probably under-utilized by beginners. (For a parallel construction involving assignment to a record, see Section 13.13.) For example: set {x, y, z} to {1, 2, 3}
z -- 3, and can you guess what x and y are?
It sounds from their descriptions as if set and copy must be completely interchangeable. In most cases, they are; but with regard to four types of value—lists, records, dates, and script objects—they are not. This point will be covered in subsequent chapters. For other datatypes, you may use whichever command you prefer; I prefer set. There is no simple assignment operator, such as equals sign (=). You cannot, for example, perform an assignment like this: x = 5 That is a comparison, and returns a boolean result revealing whether x is already 5. The fact that such code is legal (and therefore does not cause a compile-time error) but is not an assignment (as any mildly experienced programmer would expect) is a frequent cause of bugs in my scripts. See Section 5.3. To retrieve the value of a variable (or fetch the value, or use the value, or whatever you want to call it), simply use the variable's name in code. As with most computer languages, there is no problem retrieving from and assigning to the same variable in a single statement: set x to x + 1 There is no circularity, because first the value of x is retrieved, and 1 is added to that value; then the result of this addition is assigned to x, replacing the original value. The result of a line consisting of just the name of a variable is the value of that variable. So, for example: set x to 5 x The result of that script is 5. This can be useful when you want to employ the implicit result of a script as a way of testing or debugging (see Section 6.2.2). It is possible to retrieve a variable's value by using the get command: set x to (get x) + 1 But no one ever talks this way in real life, and as far as I know this use of get with a variable adds nothing. However, get with an object reference is another matter; see Section 10.3. |
[ Team LiB ] |