WHERE

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.

Calling Sequence

Result = WHERE( Array_Expression [, Count] )

Arguments

Array_Expression

The array to be searched. This argument can be of any basic type except string. Both the real and imaginary parts of a complex number must be zero for the number to be considered zero.

Count

A named variable that, on exit, is set to the number of nonzero elements found in Array_Expression . This value is returned as a longword integer.

When WHERE Returns -1

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.

Example

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.

a=[1,2,3,4,5,5,4,3,2,1] ; Compare array, a, and

b=5 ; scalar, b

result=where(a eq b)

help,result & print,result

RESULT LONG = Array[2] ; Result= 4,5 as expected

4 5

 

c=[1,2,3,4,5,5,4,3,2,1] ; Now compare two arrays

d=[0,2,4] ; of different lengths

result=where(c eq d)

help,result & print,result

RESULT LONG = Array[1] ; Result = 1; not what

1 ; user would expect

See Also

UNIQ