Previous Building IDL Applications: Arrays Next

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 Subscript Ranges for complete details. For information on when you should not use subscript ranges, see Avoid Using Range Subscripts.

Table 15-2: Introduction to Subscript Expression Structures 

Table 15-2: Introduction to Subscript Expression Structures 
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 Clipping for details.


Note
Array operations are much more efficient than loops. See Use Vector and Array Operations for details.

  IDL Online Help (June 16, 2005)