Skip to main content

4.7) where Function (cont'd)


When using WHERE, the count variable is very important. If WHERE does not find any elements which satisfy the specified criteria then it returns a value of -1. In the latest versions of IDL, if you use -1 as a reference to index an array, then this will return the last element of the array.

x = indgen(10)
y = WHERE(x gt 20, count)
print, x(y)

IDL will print "9" to the screen, which is clearly not what we want. Therefore, you must check that count is non-zero before using the result of a WHERE statement.

IF count ne 0 THEN print, x(y) ELSE print, 'WHERE found no match'