The WHERE function returns a longword vector that contains the one-dimensional subscripts of the nonzero elements of Array_Expression . The length of the resulting vector is equal to the number of nonzero elements in the parameter. Frequently the result of WHERE is used as a vector subscript to select elements of an array using given criteria. If all elements of Array_Expression are zero the result of WHERE is a scalar integer with the value -1.
The system variable !ERR is set to the number of nonzero elements. This effect is for compatibility with previous versions of IDL and should not be used in new code. Use the COUNT argument to return this value instead.
If all the elements of Array_Expression are zero, WHERE returns a scalar integer with a value of -1. Attempting to use this result as an index into another array results in a "subscripts out of bounds" error. In situations where this is possible, code similar to the following can be used to avoid errors:
index = WHERE(array, count) ; Use Count to get the number of nonzero elements.
IF count NE 0 THEN result = array[index]
;
Only subscript the array if it's safe.
array = FINDGEN(100)
;
Create a 100-element, floating-point array where each element is set to the value of its subscript.
B = WHERE(array GT 20, count) ; Find the subscripts of all the elements in the array that have values greater than 20.
PRINT, count ; Print how many elements met the search criteria.
PRINT, B ; Print the subscripts of found elements.
values = array[B] ; Make an array containing the values of array referred to by B.
NOTE: The WHERE function behaves differently with different kinds of array expressions. For instance, if a relational operator is used to compare an array, A, with a scalar, B, then every element of A is searched for B. However, if a relational operator is used to compare two arrays, C and D, then a comparsion is made between each corresponding element (i.e. Ci & Di, Ci+1 & Di+1, etc) of the two arrays. If the two arrays have different lengths then a comparison is only made up to the number of elements for the shorter array.
a=[1,2,3,4,5,5,4,3,2,1] ; Compare array, a, and
RESULT LONG = Array[2] ; Result= 4,5 as expected
c=[1,2,3,4,5,5,4,3,2,1] ; Now compare two arrays
d=[0,2,4] ; of different lengths