comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: keyword_set bug or feature
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: keyword_set bug or feature [message #4205] Mon, 15 May 1995 00:00
chase is currently offline  chase
Messages: 62
Registered: May 1993
Member
>>>> > "Rob" == Stephen O'Connell <soc@festival.ed.ac.uk> writes:

Rob> Keyword is more than just 1/0 switches! And if you are using them
Rob> to pass values, you just might mistakenly set one equal to zero.
Rob> Andd although its true that you can use n_elements, you're still
Rob> buggered if you have more than one keyword...so it is a bug
Rob> really, in my opinion. Maybe ver. 4.0 has it sorted - does
Rob> anyone know ?

It is not a bug. KEYWORD_SET works exactly as documented in the "IDL
Reference Guide". Furthermore, the guide states: "This function is
especially useful in user-written procedures and functions that
process keywords that are interpreted as being either true (keyword
present and nonzero) or false (keyword was not used, or was set to
zero)." Thus, its purpose is not for checking if a keyword is used to
pass values. Just because KEYWORD_SET does not do what you wish does
not mean it has a bug. Use

N_ELEMENTS(var) NE 0

to check for defined keywords. You can make your own function
"KEYWORD_DEFINED()" if you think it is more convenient than
"N_ELEMENTS(var) NE 0".

Chris

--
===============================
Bldg 24-E188
The Applied Physics Laboratory
The Johns Hopkins University
Laurel, MD 20723-6099
(301)953-6000 x8529
chris.chase@jhuapl.edu
Re: keyword_set bug or feature [message #4212 is a reply to message #4205] Sat, 13 May 1995 00:00 Go to previous message
rivers is currently offline  rivers
Messages: 228
Registered: March 1991
Senior Member
In article <PHIL.95May12090139@peace.med.ohio-state.edu>, phil@peace.med.ohio-state.edu (Phil) writes:
> Thanks to those who responded to my ? concerning common blocks.
>
> I just can across another 'feature' and was wondering if others felt
> that it is really a 'bug' or if there is some way to over come it. It
> has to do with keywords in functions or procedures. I use several and
> check to see if the are set using
>
> IF NOT(keyowrd_set(key)) THEN BEGIN
> ;set a default value here
> ENDIF
>
> The problem comes in if the user wants to set key = 0. If so then it
> appears to the above test that the keyword is not set even though in
> the function call the user typed
>

keyword_set is intended for use with switches, i.e. parameters which can either
be 0 or 1. 0 or not present means switch not set, 1 means switch set.

If you want to detect that the keyword is present, even if the value is zero,
then you should use n_elements().

____________________________________________________________
Mark Rivers (312) 702-2279 (office)
CARS (312) 702-9951 (secretary)
Univ. of Chicago (312) 702-5454 (FAX)
5640 S. Ellis Ave. (708) 922-0499 (home)
Chicago, IL 60637 rivers@cars3.uchicago.edu (Internet)
Re: keyword_set bug or feature [message #4214 is a reply to message #4212] Fri, 12 May 1995 00:00 Go to previous message
peter is currently offline  peter
Messages: 80
Registered: February 1994
Member
Phil (phil@peace.med.ohio-state.edu) wrote:
: Thanks to those who responded to my ? concerning common blocks.

: I just can across another 'feature' and was wondering if others felt
: that it is really a 'bug' or if there is some way to over come it. It
: has to do with keywords in functions or procedures. I use several and
: check to see if the are set using

: IF NOT(keyowrd_set(key)) THEN BEGIN
: ;set a default value here
: ENDIF

: The problem comes in if the user wants to set key = 0. If so then it
: appears to the above test that the keyword is not set even though in
: the function call the user typed
:
: Result = somefunction(var,key=0)

: What gives? Can I just add key = key + 1 and key = key - 1 around the
: testing if statement?


This is the specified behavior of keyword_set, and yes, it has caused
problems for others (i.e. me!). The solution to your problem is to
used n_elements(key) to test whether any value was set, or no value
at all. n_elements will return 0 on an undefined variable, and 1
on any scalar, including 0.

Peter
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Plotting to file
Next Topic: Re: Bad pixmap error, pixmap allocation, keywords

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:41:50 PDT 2025

Total time taken to generate the page: 0.00692 seconds