Up | Next | Tail |
These statements have the syntax
The \(\langle \)expression\(\rangle \) on the left side is normally the name of a variable, an operator symbol with its list of arguments filled in, or an array name with the proper number of integer subscript values within the array bounds. For example:
a1 := b + c h(l,m) := x-2*y (where h is an operator) k(3,5) := x-2*y (where k is a 2-dim. array)
More general assignments such as a+b := c are also allowed. The effect of these is explained in Section 11.2.5.
An assignment statement causes the expression on the right-hand-side to be evaluated. If the left-hand-side is a variable, the value of the right-hand-side is assigned to that unevaluated variable. If the left-hand-side is an operator or array expression, the arguments of that operator or array are evaluated, but no other simplification done. The evaluated right-hand-side is then assigned to the resulting expression. For example, if a is a single-dimensional array, a(1+1) := b assigns the value b to the array element a(2).
If a semicolon is used as the terminator when an assignment is issued as a command (i.e. not as a part of a group statement or procedure or other similar construct), the left-hand side symbol of the assignment statement is printed out, followed by a “:=”, followed by the value of the expression on the right.
It is also possible to write a multiple assignment statement:
In this form, each \(\langle \)expression\(\rangle \) but the last is set to the value of the last \(\langle \)expression\(\rangle \). If a semicolon is used as a terminator, each expression except the last is printed followed by a “:=” ending with the value of the last expression.
In some cases, it is desirable to perform an assignment in which both the left- and right-hand sides of an assignment are evaluated. In this case, the set statement can be used with the syntax:
For example, the statements
j := 23; set(mkid(a,j),x);
assigns the value x to a23. (See also mkid.)
To remove a value from such a variable, the unset statement can be used with the syntax:
For example, the statement
j := 23; unset(mkid(a,j));
clears the value of a23.
The command unset also acts like an indirect version of clear for operator values; for each operator value it clears the value assigned to the operator value, rather than the operator value itself. For example
operator a, b; a(1) := x; b(1) := 4; a(2) := y; b(2) := 3; set(a(1), b(1)); % performs x := 4 set(a(2), b(2)); % performs y := 3 x*y; % returns 12 unset a(1), a(2); % performs clear x, y x*y; % returns x*y {a(1),b(1),a(2),b(2)}; % returns {x,4,y,3}
Note that the unset command clears the values of x and y, but leaves a(1), b(1), a(2), b(2) assigned.
Up | Next | Front |