6.5.
|
Chapter 7.
|
7.2. Assignment and Retrieval
If a variable is a labelled shoebox (see "Variables" in Chapter 6), then to
assign
a value to a variable is to put something into the shoebox. If the variable already has a value, that value is
Assignment is performed with one of two commands: set or copy .
Syntaxset variableName to value DescriptionAssigns 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 synonym used.
Syntaxcopy value to variableName DescriptionAssigns 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 . These abbreviations were designed to accommodate HyperCard users, who were habituated to this idiom. There is no simple assignment operator, such as the equals sign ( = ). You cannot, for example, perform an assignment like this: x = 5
That is a comparison, and returns a boolean result
7.2.1. Set by Reference
As they both perform assignment, you might think
set
and
copy
must be completely interchangeable. In most cases, they are; but with regard to four types of valuelists, records, dates, and script objectsthey are not. With these data types,
set
sets
by reference
, meaning that you can end up with more than one
The reason why these four data types are singled out for special treatment is that they are the only kinds of value that can be mutated in place. Thus, after a set by reference, whatever mutation is performed upon such a value under one of its names applies to it under its other
set L to {1, 2, 3}
set LL to L --
set by reference
set end of L to 4 --
mutate a list in place
LL --
{1, 2, 3, 4}
, because it is the same list as L
For other datatypes, use whichever command you prefer; I habitually use set . 7.2.2. Multiple Assignment
In an assignment,
variableName
and
value
can
set {x, y, z} to {1, 2, 3}
z --
3
, and can you guess what
x
and
y
are?
7.2.3. RetrievalTo 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 the value of x is first 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 see the implicit result of a script for testing or debugging (see "Implicit Result" in Chapter 5). 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 "Get" in Chapter 11. |