Reputation: 39
I am new to IDL and find the KEYWORD_SET
difficult to grasp. I understand that it is a go no go switch. I think its the knocking on and off part that I am having difficulty with. I have written a small program to master this as such
Pro get_this_done, keyword1 = keyword1
WW=[3,6,8]
PRINT,'WW'
print,WW
y= WW*3
IF KEYWORD_Set(keyword1) Then BEGIN
print,'y'
print,y
ENDIF
Return
END
WW
prints but print, y
is restricted by the keyword. How do I knock off the keyword to allow y
to print.
Silly little question, but if somebody can indulge me, it would be great.
Upvotes: 0
Views: 519
Reputation: 6044
Keywords are simply passed as arguments to the function:
get_this_done, KEYWORD1='whatever'
or also
get_this_done, /KEYWORD1
which will give KEYWORD1 the INT value of 1 inside the function. Inside the function KEYWORD_SET will return 1 (TRUE) when the keyword was passed any kind of value - no matter whether it makes sense or not.
Thus as a side note to the question: It often is advisable to NOT use KEYWORD_SET
, but instead resort to a type query:
IF SIZE(variable, /TNAME) EQ 'UNDEFINED' THEN $
variable = 'default value'
It has the advantage that you can actually check for the correct type of the keyword and handle unexpected or even different variable types:
IF SIZE(variable, /TNAME) NE 'LONG' THEN BEGIN
IF SIZE(variable, /TNAME) EQ 'STRING' THEN $
PRINT, "We need a number here... sure that the cast to LONG works?"
variable = LONG(variable)
ENDIF
Upvotes: 1
Reputation: 298
After compiling the routine, type something like
get_this_done,KEYWORD1=1b
where the b
after the one sets the numeric value to a BYTE
type integer (also equivalent to TRUE
). That should cause the y-variable to be printed to the screen.
The KEYWORD_SET function will return a TRUE
for lots of different types of inputs that are basically either defined or not zero. The IF loop executes when the argument is TRUE
.
Upvotes: 1