Using Scalar Values as Subscripts
Scalar quantities in IDL can be thought of as the first element of an array with one dimension. They can be subscripted with a zero reflecting the first and only position. Therefore,
IDL prints:
If we redefine the first element of A:
IDL prints:
Note
You cannot subscript a variable that has not yet been defined. Thus, if the variable B has not been previously defined, the statement:B[0] = 9
will fail with the error "variable is undefined."
Subscripting Arrays Using Scalar Values
The subscripted variable can have either a scalar or array subscript with the form:
If the subscript expression is a scalar value, a single element of the specified array is set to the value of the scalar expression. The expression can 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. Some examples of assigning scalar expressions to subscripted variables are:
; Set element 100 of data to value. data[99] = 1.234999 ; Store string in an array. aName must be a string array or an ; error will result. aName[index] = 'Joe' ; Set element [X, Y] of the 2-dimensional array image to the value ; contained in pixel. image[X, Y] = pixel
If the subscript expression is an array, the scalar value is stored in the elements of the array whose subscripts are elements of the subscript array. For example, the following statement zeroes the four specified elements of data: data[3], data[5], data[7] and data[9]:
The subscript array is converted to integer type if necessary before use. 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. Note that a common error is to use a negative scalar subscript (e.g., A[-1]). Using this type of subscript causes an error. Negative array subscripts (e.g., A[[-1]]) do not cause errors.
When a subscripted variable reference appears in an expression, the values of the selected array elements are extracted. For example, the following statements extract the first two values from array by subscripting with a second array (indices) and store the values in the variable new_array:
IDL prints:
See the following sections for more information on array subscripts and clipping.