Idl Error Attempt To Subscript Out Of Range
Contents |
IDL> indices = [2, 5, 9] IDL> Print, out of range subscript encountered idl array[indices] 2 5 9 All as expected. But, if
Illegal Subscript Range
I make a typing mistake and do this, why is there no error? IDL> idl for loop indices = [2, 5, 99] IDL> Print, array[indices] 2 5 9 In fact, any out-of-bounds index is truncated to the nearest valid index. IDL> indices = [-20, 15, 168] IDL> Print, array[indices] 0 9 9 Can this be right!? ANSWER: Well, it is hard to say if it is "right" or not. It is a long-time feature of IDL, although not well-known by even the most experienced IDL users. Note that this kind of out-of-bounds subscript truncating only occurs if the subscript indices are in an vector. In other words, the following code does generate an error. IDL> Print, array[99] % Attempt to subscript ARRAY with
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 http://www.idlcoyote.com/code_tips/oobindices.html 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 12945 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 http://northstar-www.dartmouth.edu/doc/idl/html_6.2/Assignment_Operations_and_Arrays.html 1111245 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 12040 Note - Note the use of the double brackets. Attempting to assign zeros to the 3rd and 5th element of the array usingarrOne[2, 4] = 0 results in an error: "Attempt to subscript ARRONE with