Assignment Statement
The assignment statement stores a value in a variable. There are four forms of the assignment statement. They are described in detail in this section.
Table 4-1: Assignment Statement Forms summarizes the four forms of assignment statements.
Form 1
The first (and most basic) form of the assignment statement has the form:
variable = expression
; Stores the value of the expression in the variable.
The old value of the variable, if any, is discarded and the value of the expression is stored in the variable. The expression on the right side may be of any type or structure. Some examples of the basic form of the assignment statement are:
; Stores the value of the expression in MMAX.
MMAX = 100 * X + 2.987
; Stores the string 'MARY' in the variable NAME.
NAME = 'MARY'
; ARR is now a 100-element floating-point array.
ARR = FLTARR(100)
; Discards elements 0 to 49 of ARR. ARR is now a 50-element array.
ARR = ARR(50:*)
Form 2
The second type of assignment statement has the form:
; Stores the scalar in an element of the array variable.
variable(subscripts) = scalar_expression
Here, a single element of the specified array is set to the value of the scalar expression. The expression may be of any type and is converted, if necessary, to the type of the variable. The variable on the left side must be either an array or a file variable.
; Sets element (100) of DATA to value.
DATA(100) = 1.234999
; Stores a string in the array. NAME must be a string array or
; an error will result.
NAME(INDEX) = 'JOE'
Using Array Subscripts with the Second Form
If the subscript expression is an array, the scalar value will be stored in the elements of the array whose subscripts are elements of the subscript array. For example, the statement:
DATA( [3, 5, 7, 9] ) = 0
will zero the four specified elements of DATA: DATA(3), DATA(5), DATA(7), and DATA(9).
The subscript array is converted to type LONG if necessary before use. Negative subscripts, or subscripts greater than the highest subscript, result in the first and the last element of the array being changed respectively.
The WHERE function may frequently be used to select elements to be changed. For example, the statement:
DATA(WHERE(DATA LT 0)) = -1
will set all negative values of DATA to –1 without changing the positive values. The result of the function WHERE(DATA LT 0) is a vector composed of the subscripts of the negative values of DATA. Using this vector as a subscript changes all the negative values to –1 in DATA. Note that if the WHERE function finds no eligible elements, it returns a 1-element vector equal to –1; using this result as a subscript vector changes no elements of the subscripted array; it results in a “subscript out of range” error as negative subscripts are not allowed. For more information on the WHERE function, see the PV‑WAVE Reference.
Form 3
The third type of assignment statement is similar to the second, except the subscripts specify a range in which all elements are set to the scalar expression.
; Stores the scalar in the elements of the array variable
; designated by the subscript range.
variable(subscript_range) = scalar_expression
A subscript range specifies a beginning and ending subscript. The subscripts are separated by the colon character. An ending subscript equal to the size of the dimension minus one may be written as *.
For example, ARR(I:J) denotes those points in the vector ARR with subscripts between I and J. I must be less than J and greater than or equal to zero. J must be less than the size of the array dimension. ARR(I:*) denotes the points in ARR from ARR(I) to the last point.
For more information on subscript ranges, see
"Subscript Ranges".
Assuming the variable B is a 512-by-512 byte array, some examples are:
; Stores ones in the ith row.
B(*, I) = 1
; Stores ones in the jth column.
B(J, *) = 1
; Zeroes all the rows of the columns 200 through 220 of the array
; B.
B(200:220, *) = 0
; Stores the value 100 in all the elements of the array B.
B(*) = 100.
Form 4
The fourth type assignment statement is of the form:
; Inserts the array expression into the array variable starting
; at the element designated by the subscripts.
variable(subscripts) = array
Note that this form is syntactically identical to the second type of assignment statement, except the expression on the right is an array instead of a scalar. This form of the assignment statement is used to insert one array into another.
The array expression on the right is inserted into the array appearing on the left side of the equal sign, starting at the point designated by the subscripts.
For example, to insert the contents of an array called A into an array called B, starting at point B(13,24):
; If A is a 5-column by 6-row array, elements B(13:17, 24:29)
; will be replaced by the contents of the array A.
B(13, 24) = A
Another example moves a subarray from one position to another:
; A subarray of B, specifically the columns 200 to 300 and
; rows 300 to 400, is moved to columns 100 to 200 and rows
; 200 to 300, respectively.
B(100, 200) = B(200:300, 300:400)
Using Array Subscripts with the Fourth Form
If the subscript expression applied to the variable is an array and an array appears on the right side of the statement:
var(array) = array
elements from the right side are stored in the elements designated by the subscript vector. Only those elements of the subscripted variable whose subscripts appear in the subscript vector are changed.
For example, the statement:
B( [2, 4, 6 ] ) = [4, 16, 36]
is equivalent to the following series of assignment statements:
B(2) = 4 & B(4) = 16 & B(6) = 36
Subscript elements are interpreted as if the subscripted variable is a vector. For example if A is a 10-by-n matrix, the element A(i,j) has the subscript (i+ 10j). The subscript array is converted to type LONG before use if necessary.
As described above for the second type of assignment statement, elements of the subscript array that are negative or larger than the highest subscript are ignored and the corresponding element of the array on the right side of the equal sign is skipped.
As another example, assume that the vector DATA contains data elements and that a data drop-out is denoted by a negative value. In addition, assume that there are never two or more adjacent drop-outs.
The following statements will replace all drop-outs with the average of the two adjacent good points:
; Subscript vector of drop-outs.
BAD = WHERE(DATA LT 0)
; Replace drop-outs with average of previous and next point.
DATA(BAD) = (DATA(BAD - 1) + DATA(BAD + 1)) / 2
In this example:
Elements of the vector
BAD are set to the subscripts of the points of
DATA that are drop-outs using the WHERE function. The WHERE function returns a vector containing the subscripts of the non-zero elements of its (
DATA LT 0). This Boolean expression is a vector that is non-zero where the elements of
DATA are negative and is zero if positive.
The expression
DATA(BAD – 1) is a vector which contains the subscripts of the points immediately preceding the drop-outs, while similarly, the expression
DATA(BAD + 1) is a vector containing the subscripts of the points immediately after the drop-outs.
The average of these two vectors is stored in
DATA(BAD)— the points that originally contained drop-outs.
Associated Variables in Assignment Statements
A special case occurs when using an associated file variable in an assignment statement. For additional information regarding the ASSOC function, see
Chapter 9: Working with Data Files. When a file variable is referenced, the last (and possibly only) subscript denotes the record number of the array within the file. This last subscript must be a simple subscript. Other subscripts and subscript ranges, except the last, have the same meaning as when used with normal array variables.
An implicit extraction of an element or subarray in a data record may also be performed:
; Variable A associates the file open on unit 1 with records of
; 200-element floating point vectors.
A = ASSOC(1, FLTARR(200))
; X is set to the first 100 points of record number 2, the third
; record of the file.
X = A(0:99, 2)
; Sets the 24th point of record 16 to 12.
A(23, 16) = 12
; Points 10 to 199 of record 12 are incremented. Points 0 to 9 of
; that record remain unchanged.
A(10, 12) = A(10:*, 12) + 1