Language > Variables > Assignment Operations and Arrays

# Assignment Operations and Arrays

The following table shows the variations possible in expressions containing array and scalar subscripts. The result of the assignment operation depends upon the dimensionality of the subscript.

Note: A subscript structure can also be composed of a range of elements. If expression is scalar, it is inserted into the subarray. If Variable[Range] and Array are the same size, elements of Array specified by Range are inserted in Variable. It is illegal if Variable[Range] and Array are different sizes. See Array Subscript Ranges for complete details. For information on when you should not use subscript ranges, see Avoid Using Range Subscripts for Assignment.

Note: When assigning elements to an array, the elements are converted to the data type of the array variable. Depending upon the size of the values compared to the data type's range, this may result in the data wrapping around or losing precision. For example, assigning the value 257 to a byte array will result in the value 1. As another example, assigning any non-zero value to a boolean array will give the value true (1).

Syntax Structure | Description
Variable[ScalarSubscripts] = ScalarExpression | Expression is stored in a single element of Variable. arrOne = [1, 2, 3, 4, 5] arrOne[2] = 9 PRINT, arrOne 1 2 9 4 5
Variable[ScalarSubscripts] = ArrayExpression | Expression array is inserted in Variable array beginning at point indicated by subscript. arrOne = [1, 2, 3, 4, 5] arrTwo = [11, 12] arrOne[1] = ArrTwo PRINT, arrOne 1 11 12 4 5 Note: An “out of range subscript” error will occur if you attempt to insert `arrTwo` elements into non-existent elements of `arrOne`. For example `arrOne[4] = ArrTwo` fails.
Variable[ArraySubscripts] = ScalarExpression | Expression scalar is stored in designated elements of Variable. Other array elements are unchanged. arrOne = [1, 2, 3, 4, 5] arrOne[[2, 4]] = 0 PRINT, arrOne 1 2 0 4 0 Note: Note the use of the double brackets. Attempting to assign zeros to the 3rd and 5th element of the array using `arrOne[2, 4] = 0` results in an error: “`Attempt to subscript ARRONE with <INT(4)> is out of range.`” IDL interprets this as attempting to modify a single element in the 3rd column and 5th row, which does not exist.
Variable[ArraySubscripts] = ArrayExpression | Elements of Expression are stored in designated elements of Variable. arrOne = [1, 2, 3, 4, 5] arrOne[[0, 2]] = [111,333] PRINT, arrOne 111 2 333 4 5 Note: Elements of the subscript array that are negative, or greater than the highest subscript of the subscripted array, are clipped to the target array boundaries. For example, `arrOne[[-1, 2]] = [111,333]` has the same result as `arrOne[[0,2]]`. See Array Subscripts and Clipping for details.