CASE statement [message #26552] |
Fri, 07 September 2001 13:17  |
K Banerjee
Messages: 14 Registered: September 2001
|
Junior Member |
|
|
In IDL, is it possible to use mulitple expressions with a single
statement?
Something like:
PRO CASE1, Tag
case Tag of
;; The following line does not work.
('TIFF') or ('tiff'): begin
print, "TAG is TIFF"
end
else: begin
print, "TAG is UNKNOWN"
end
endcase
end
Thanks.
K. Banerjee
--
"One World, One Web, One Program" -- Microsoft Promotional Ad
"Ein Reich, Ein Volk, Ein Fuhrer" -- Adolf Hitler
|
|
|
Re: CASE statement [message #26938 is a reply to message #26552] |
Wed, 03 October 2001 23:51   |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
David Fanning wrote:
>
> George McCabe (george.mccabe@gsfc.nasa.gov) writes:
>
>> ;+
>> ; PURPOSE:
>> ; "On or Off", turn any variable into a strict logical type
>> ; with only two values 0 or 1
>> ;
>
> Impressive code, George. But couldn't this be
> more simply done with this:
>
> on_off = Keyword_Set(variable)
>
Hi David and all others,
the most problem with all these functions arg_present, n_elements,
keyword_set
is that's most beginners believe that's some of them only works by
keywords
and some only for positional parameters.
To learn something about IDL and its parameters here are two exercises
of our course.
regards
Reimar
1)
There are three functions given
FUNCTION test1,minimum=min_val
IF KEYWORD_SET(min_val) THEN RETURN,1 ELSE RETURN,0
END
FUNCTION test2,minimum=min_val
IF ARG_PRESENT(min_val) THEN RETURN,1 ELSE RETURN,0
END
FUNCTION test3,minimum=min_val
IF N_ELEMENTS(min_val) GT 0 THEN RETURN,1 ELSE RETURN,0
END
Fill out the form:
CALL | test1 | test2 |test3
____________________________________________________________ ____
PRINT, testX( ) | | |
____________________________________________________________ ____
PRINT, testX(minimum=0) | | |
____________________________________________________________ ____
PRINT, testX(minimum=10) | | |
____________________________________________________________ ____
PRINT, testX(minimum=-10) | | |
____________________________________________________________ ____
mv=0 & PRINT, testX(minimum=mv) | | |
____________________________________________________________ _____
mv=10 & PRINT, testX(minimum=mv) | | |
____________________________________________________________ _____
PRINT, testX(minimum=mv2) | | |
____________________________________________________________ _____
2)
There are three functions given
FUNCTION test1,min_val
IF KEYWORD_SET(min_val) THEN RETURN,1 ELSE RETURN,0
END
FUNCTION test2,min_val
IF ARG_PRESENT(min_val) THEN RETURN,1 ELSE RETURN,0
END
FUNCTION test3,min_val
IF N_ELEMENTS(min_val) GT 0 THEN RETURN,1 ELSE RETURN,0
END
Fill out the form:
CALL | test1 | test2 |test3
____________________________________________________________ ____
PRINT, testX( ) | | |
____________________________________________________________ ____
PRINT, testX(0) | | |
____________________________________________________________ _____
PRINT, testX(10) | | |
____________________________________________________________ ____
PRINT, testX(-10) | | |
____________________________________________________________ ____
mv=0 & PRINT, testX(mv) | | |
____________________________________________________________ _____
mv=10 & PRINT, testX(mv) | | |
____________________________________________________________ _____
PRINT, testX(mv2) | | |
____________________________________________________________ _____
--
Reimar Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg1/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
http://www.fz-juelich.de/zb/text/publikation/juel3786.html
============================================================ ======
read something about linux / windows
http://www.suse.de/de/news/hotnews/MS.html
|
|
|
|
Re: CASE statement [message #27021 is a reply to message #26552] |
Mon, 08 October 2001 09:55  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
JD Smith (jdsmith@astro.cornell.edu) writes:
> The paranoid may therefore use:
>
> if n_elements(key) ne 0 then if keyword_set(key[0]) then do_something
>
> or do you contend that key=[1,0] should also be "false"?
Thought I would just comment on this tread, in general.
Those of us who write a lot of IDL programs
have had to learn (of necessity, often) to
write defensively. For example, I never check
a string in a CASE statement without wrapping it
in a STRUPCASE call. But, you can either write
VERY defensive code, or you can get your work
done. :-)
Keyword_Set is surely one of the most abused
functions out there, but even using it improperly
doesn't cause many problems, most of the time.
I'm all for proper programming practice, and
I think I do my share to let people know the
"right" way to do things (Stein Vidar be dammed) :-).
But, folks, the people using these programs
are adults. *We* are adults. We know both we
and they are going to make mistakes.
When that happens, fix the error and move
on. Spending hours anticipating that someone
is going to pass an array, of all things, to
one of our keywords is wasted work, it seems
to me. If it happens once, I'd fix it. If it
happened twice. I'd fix it and think about it.
But until they pass me three times down the line,
I don't give up the center of the court for
anything. :-)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: CASE statement [message #27023 is a reply to message #26552] |
Mon, 08 October 2001 09:27  |
John-David T. Smith
Messages: 384 Registered: January 2000
|
Senior Member |
|
|
Timm Weitkamp wrote:
>
> Paul van Delst <paul.vandelst@noaa.gov> wrote in messagenews:<3BBE2163.F73A162D@noaa.gov>...
>> "Pavel A. Romashkin" wrote:
>>>
>>> Timm Weitkamp wrote:
>>>>
>>>> It actually also has some counterintuitive features. This one, for
>>>> example:
>>>>
>>>> IDL> PRINT, KEYWORD_SET([0])
>>>> 1
>>>
>>> Correct me if I am wrong, but the PURPOSE of KEYWORD_SET is to check if
>>> a KEYWORD is SET. Settable keywords are those that you either set or
>>> don't, either /K_SET or K_SET=0. As such, they should not be set to
>>> arrays. As far as I can see, Keyword_Set is a convenience function. For
>>> value passing keywords, N_elements should be used.
>>> So, it looks to me that the name of this function means exactly what it
>>> is designed for.
>>
>> Excellent point.
>>
>> paulv
>
> Pavel and Paul,
>
> I mostly agree with you. But when you're programming a routine, even
> if you do use KEYWORD_SET in the strictly limited context that it
> should be, it is still possible that the user of the routine
> accidentally passes [0] (or [1]) instead of 0 (or 1) if the keyword is
> passed as an expression and not as an explicit value as in Pavel's
> example. Of course you may then blame the user, but that is of limited
> help.
The paranoid may therefore use:
if n_elements(key) ne 0 then if keyword_set(key[0]) then do_something
or do you contend that key=[1,0] should also be "false"?
JD
|
|
|
Re: CASE statement [message #27039 is a reply to message #26552] |
Mon, 08 October 2001 00:19  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Timm Weitkamp wrote:
>
> Paul van Delst <paul.vandelst@noaa.gov> wrote in messagenews:<3BBE2163.F73A162D@noaa.gov>...
>> "Pavel A. Romashkin" wrote:
>>>
>>> Timm Weitkamp wrote:
>>>>
>>>> It actually also has some counterintuitive features. This one, for
>>>> example:
>>>>
>>>> IDL> PRINT, KEYWORD_SET([0])
>>>> 1
>>>
>>> Correct me if I am wrong, but the PURPOSE of KEYWORD_SET is to check if
>>> a KEYWORD is SET. Settable keywords are those that you either set or
>>> don't, either /K_SET or K_SET=0. As such, they should not be set to
>>> arrays. As far as I can see, Keyword_Set is a convenience function. For
>>> value passing keywords, N_elements should be used.
>>> So, it looks to me that the name of this function means exactly what it
>>> is designed for.
>>
>> Excellent point.
>>
>> paulv
>
> Pavel and Paul,
>
> I mostly agree with you. But when you're programming a routine, even
> if you do use KEYWORD_SET in the strictly limited context that it
> should be, it is still possible that the user of the routine
> accidentally passes [0] (or [1]) instead of 0 (or 1) if the keyword is
> passed as an expression and not as an explicit value as in Pavel's
> example. Of course you may then blame the user, but that is of limited
> help.
>
> Timm
Dear Timm,
you can never use a one element vector by IF Statements or e.g. by
the WHERE command for comparison.
a=findgen(10)
print,where(a eq [1])
IDL > -1
IDL> if [1] eq [1] then print,'aa'
% Expression must be a scalar in this context: <BYTE Array[1]>.
% Execution halted at: $MAIN$
Of course it's not fine that's KEYWORD_SET don't crash if it is
used with [1] or [0].
But if your arguments could be arrays then
it is not the best way to use KEYWORD_SET. As Pavel explained
this function is only useful to distinguish between true (1) and
false (0). In addition the variable for keyword_set could be
a positional parameter too.
This is shown by the two excercises. The result in both tabulars
must be the same. If you don't believe try the examples.
Reimar
--
Reimar Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg1/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
http://www.fz-juelich.de/zb/text/publikation/juel3786.html
============================================================ ======
read something about linux / windows
http://www.suse.de/de/news/hotnews/MS.html
|
|
|
Re: CASE statement [message #27045 is a reply to message #26552] |
Sat, 06 October 2001 03:01  |
weitkamp
Messages: 33 Registered: October 1998
|
Member |
|
|
Paul van Delst <paul.vandelst@noaa.gov> wrote in messagenews:<3BBE2163.F73A162D@noaa.gov>...
> "Pavel A. Romashkin" wrote:
>>
>> Timm Weitkamp wrote:
>>>
>>> It actually also has some counterintuitive features. This one, for
>>> example:
>>>
>>> IDL> PRINT, KEYWORD_SET([0])
>>> 1
>>
>> Correct me if I am wrong, but the PURPOSE of KEYWORD_SET is to check if
>> a KEYWORD is SET. Settable keywords are those that you either set or
>> don't, either /K_SET or K_SET=0. As such, they should not be set to
>> arrays. As far as I can see, Keyword_Set is a convenience function. For
>> value passing keywords, N_elements should be used.
>> So, it looks to me that the name of this function means exactly what it
>> is designed for.
>
> Excellent point.
>
> paulv
Pavel and Paul,
I mostly agree with you. But when you're programming a routine, even
if you do use KEYWORD_SET in the strictly limited context that it
should be, it is still possible that the user of the routine
accidentally passes [0] (or [1]) instead of 0 (or 1) if the keyword is
passed as an expression and not as an explicit value as in Pavel's
example. Of course you may then blame the user, but that is of limited
help.
Timm
|
|
|
Re: CASE statement [message #27048 is a reply to message #26552] |
Fri, 05 October 2001 14:08  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
"Pavel A. Romashkin" wrote:
>
> Timm Weitkamp wrote:
>>
>> It actually also has some counterintuitive features. This one, for
>> example:
>>
>> IDL> PRINT, KEYWORD_SET([0])
>> 1
>
> Correct me if I am wrong, but the PURPOSE of KEYWORD_SET is to check if
> a KEYWORD is SET. Settable keywords are those that you either set or
> don't, either /K_SET or K_SET=0. As such, they should not be set to
> arrays. As far as I can see, Keyword_Set is a convenience function. For
> value passing keywords, N_elements should be used.
> So, it looks to me that the name of this function means exactly what it
> is designed for.
Excellent point.
paulv
--
Paul van Delst Religious and cultural
CIMSS @ NOAA/NCEP purity is a fundamentalist
Ph: (301)763-8000 x7274 fantasy
Fax:(301)763-8545 V.S.Naipaul
|
|
|
Re: CASE statement [message #27050 is a reply to message #26552] |
Fri, 05 October 2001 12:03  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Timm Weitkamp wrote:
>
> It actually also has some counterintuitive features. This one, for
> example:
>
> IDL> PRINT, KEYWORD_SET([0])
> 1
Correct me if I am wrong, but the PURPOSE of KEYWORD_SET is to check if
a KEYWORD is SET. Settable keywords are those that you either set or
don't, either /K_SET or K_SET=0. As such, they should not be set to
arrays. As far as I can see, Keyword_Set is a convenience function. For
value passing keywords, N_elements should be used.
So, it looks to me that the name of this function means exactly what it
is designed for.
Cheers,
Pavel
|
|
|
Re: CASE statement [message #27051 is a reply to message #26938] |
Fri, 05 October 2001 11:44  |
weitkamp
Messages: 33 Registered: October 1998
|
Member |
|
|
Reimar,
May I summarize the first statement you make like this: "KEYWORD_SET
is a nice function, but has the wrong name"?
It actually also has some counterintuitive features. This one, for
example:
IDL> PRINT, KEYWORD_SET([0])
1
The online help describes this behavior correctly, but accidentally
passing a 1-element array instead of a scalar happens easily. Wasn't
there something here recently about the pitfalls of IDL making a
difference between scalars and single-element arrays?
Timm
Timm Weitkamp
European Synchrotron Radiation Facility (ESRF), Grenoble, France
Phone: +33 4 76 88 28 86 E-mail: weitkamp@esrf.fr
Reimar Bauer <r.bauer@fz-juelich.de> wrote in message news:<3BBC0706.B983FB42@fz-juelich.de>...
> David Fanning wrote:
>>
>> George McCabe (george.mccabe@gsfc.nasa.gov) writes:
>>
>>> ;+
>>> ; PURPOSE:
>>> ; "On or Off", turn any variable into a strict logical type
>>> ; with only two values 0 or 1
>>> ;
>>
>> Impressive code, George. But couldn't this be
>> more simply done with this:
>>
>> on_off = Keyword_Set(variable)
>>
>
>
> Hi David and all others,
>
>
> the most problem with all these functions arg_present, n_elements,
> keyword_set
> is that's most beginners believe that's some of them only works by
> keywords
> and some only for positional parameters.
>
> To learn something about IDL and its parameters here are two exercises
> of our course.
>
>
> regards
>
> Reimar
>
>
> 1)
> There are three functions given
>
>
>
> FUNCTION test1,minimum=min_val
> IF KEYWORD_SET(min_val) THEN RETURN,1 ELSE RETURN,0
> END
>
>
> FUNCTION test2,minimum=min_val
> IF ARG_PRESENT(min_val) THEN RETURN,1 ELSE RETURN,0
> END
>
>
> FUNCTION test3,minimum=min_val
> IF N_ELEMENTS(min_val) GT 0 THEN RETURN,1 ELSE RETURN,0
> END
>
> Fill out the form:
>
>
>
>
> CALL | test1 | test2 |test3
> ____________________________________________________________ ____
> PRINT, testX( ) | | |
> ____________________________________________________________ ____
> PRINT, testX(minimum=0) | | |
> ____________________________________________________________ ____
> PRINT, testX(minimum=10) | | |
> ____________________________________________________________ ____
> PRINT, testX(minimum=-10) | | |
> ____________________________________________________________ ____
> mv=0 & PRINT, testX(minimum=mv) | | |
> ____________________________________________________________ _____
> mv=10 & PRINT, testX(minimum=mv) | | |
> ____________________________________________________________ _____
> PRINT, testX(minimum=mv2) | | |
> ____________________________________________________________ _____
>
>
>
>
>
> 2)
> There are three functions given
>
>
>
> FUNCTION test1,min_val
> IF KEYWORD_SET(min_val) THEN RETURN,1 ELSE RETURN,0
> END
>
>
> FUNCTION test2,min_val
> IF ARG_PRESENT(min_val) THEN RETURN,1 ELSE RETURN,0
> END
>
>
> FUNCTION test3,min_val
> IF N_ELEMENTS(min_val) GT 0 THEN RETURN,1 ELSE RETURN,0
> END
>
> Fill out the form:
>
>
>
>
> CALL | test1 | test2 |test3
> ____________________________________________________________ ____
> PRINT, testX( ) | | |
> ____________________________________________________________ ____
> PRINT, testX(0) | | |
> ____________________________________________________________ _____
> PRINT, testX(10) | | |
> ____________________________________________________________ ____
> PRINT, testX(-10) | | |
> ____________________________________________________________ ____
> mv=0 & PRINT, testX(mv) | | |
> ____________________________________________________________ _____
> mv=10 & PRINT, testX(mv) | | |
> ____________________________________________________________ _____
> PRINT, testX(mv2) | | |
> ____________________________________________________________ _____
>
>
>
> --
> Reimar Bauer
>
> Institut fuer Stratosphaerische Chemie (ICG-1)
> Forschungszentrum Juelich
> email: R.Bauer@fz-juelich.de
> http://www.fz-juelich.de/icg/icg1/
> ============================================================ ======
> a IDL library at ForschungsZentrum Juelich
> http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
>
> http://www.fz-juelich.de/zb/text/publikation/juel3786.html
> ============================================================ ======
>
> read something about linux / windows
> http://www.suse.de/de/news/hotnews/MS.html
|
|
|
Re: CASE statement [message #27053 is a reply to message #26966] |
Fri, 05 October 2001 09:25  |
george.mccabe
Messages: 10 Registered: October 2001
|
Junior Member |
|
|
fordavid, arg, keyw=keyval
print, keyword_set(keyval)
end
IDL> fordavid, 0, keyw=-1
1
(not the result i want)
thanks for the replies,
george
David Fanning <david@dfanning.com> wrote in message news:<MPG.16250eae5225cd7e9896eb@news.frii.com>...
> George McCabe (george.mccabe@gsfc.nasa.gov) writes:
>
>> ;+
>> ; PURPOSE:
>> ; "On or Off", turn any variable into a strict logical type
>> ; with only two values 0 or 1
>> ;
>
> Impressive code, George. But couldn't this be
> more simply done with this:
>
> on_off = Keyword_Set(variable)
>
> Cheers,
>
> David
|
|
|