[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Keyword discrimination

Imanol Echave (ccaeccai@sc.ehu.es) writes:

> 	The question is: How to discriminate between a keyword not set and a keyword
> set to an undefined variable? N_ELEMENTS returns 0 in the two cases.

Don't even get me started about IDL routines with misleading
names... :-)

You are correct. It is IMPOSSIBLE, using N_ELEMENTS (or
KEYWORD_SET, for that matter), to distinguish between
keywords that are unused and keywords that are set to
an undefined variable.

But sometimes you clearly want to. For example, you
would like an output keyword to contain the results of
some time-consuming calculation, but you don't want to
DO the calculation unless the caller of the routine requests
the result. You would like to know if the caller of the
function USED the keyword.

Neither N_ELEMENTS or KEYWORD_SET can furnish this information
reliably. To address this issue, RSI introduced the ARG_PRESENT
function, which *almost* does what you want it to do. It can
tell you if a "passed by reference" variable exists in a program.

To understand this, suppose I have a procedure defined like this:

   IF ARG_PRESENT(doit) THEN doit = Big_Calculation()

This will work reliably if I call the function like this
(where XX is an undefined variable):


It will fail if I call the function like this:


Here the variable argument "doit" is certainly *present* 
in my procedure (it has the value of 1), but the ARG_PRESENT
function returns 0 because the variable exists in a "passed
by value" form, not a "passed by reference" form.

Given the limitations of each of these functions, it is usually
possible to work something out in your program, often by 
using plenty of IF statements. :-)



David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/