0

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.

DavidH
  • 415
  • 4
  • 21

2 Answers2

1

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.

honeste_vivere
  • 308
  • 5
  • 11
1

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
planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • The second one listed doesn't seem to me a correct use. KEYWORD_SET is usually employed to check the value of a flag and e.g. it should never be used to set default values for a variable (as I think is in this example). Also, more important, it shouldn't be checked for undefined. The use case in the question is a perfect example, here the flag is used to decide if printing an output or not. procedure get_this_done can be called by another procedure that sets the flag to 1 and leaves the option to disable it by calling with 0. This is expected behavior that fails if checked with undefined. – Vincenzooo Mar 01 '19 at 16:35
  • I think you mis-understand my answer. My point is: it's good to use as binary flag. But if you want to check whether a variable was passed and you care about the value - and passed as the right type - then keyword_set is not of much use – planetmaker Mar 05 '19 at 13:54
  • it is possible, because I didn't understand where `variable` came from, and found it misleading. Thanks for specifying. – Vincenzooo Mar 05 '19 at 15:49