Re: Bug in N_PARAMS? [message #63974] |
Wed, 26 November 2008 07:49  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Carsten Lechte writes:
> Kenneth P. Bowman wrote:
>> to provide default values for optional parameters.
>
> I wrote this little procedure for setting default values
> that I use a zillion times a day:
>
>
> PRO defaultarg, arg, defval, VALUEGIVEN=valuegiven
>
> IF N_ELEMENTS( arg) EQ 0 THEN BEGIN
> arg = defval
> valuegiven = 0
> ENDIF ELSE valuegiven = 1
>
> END
This is a pretty good idea. I can see how it would
save some typing! :-)
Here is a slightly modified version. I'll add this,
with documentation, to the Coyote Library later today.
PRO SetDefaultValue, argument, defaultValue, BOOLEAN=boolean
; If BOOLEAN, we need just a 0 or a 1.
IF Keyword_Set(boolean) THEN BEGIN
argument = Keyword_Set(argument)
ENDIF ELSE BEGIN
; We only need assignment if the argument is undefined.
IF N_Elements(argument) EQ 0 THEN BEGIN
; If the default value is undefined, treat as BOOLEAN.
; Otherwise, assign default value to the argument.
IF N_Elements(defaultValue) EQ 0 THEN BEGIN
argument = Keyword_Set(argument)
ENDIF ELSE BEGIN
argument = defaultValue
ENDELSE
ENDIF
ENDELSE
END
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Bug in N_PARAMS? [message #63983 is a reply to message #63974] |
Tue, 25 November 2008 23:53   |
Carsten Lechte
Messages: 124 Registered: August 2006
|
Senior Member |
|
|
Kenneth P. Bowman wrote:
> to provide default values for optional parameters.
I wrote this little procedure for setting default values
that I use a zillion times a day:
PRO defaultarg, arg, defval, VALUEGIVEN=valuegiven
IF N_ELEMENTS( arg) EQ 0 THEN BEGIN
arg = defval
valuegiven = 0
ENDIF ELSE valuegiven = 1
END
then use it like this:
PRO somepro, arg1, arg2, KEY1=key1
defaultarg, arg1, 0
defaultarg, arg2, 'test'
defaultarg, key1, 2343d8
...
END
chl
|
|
|
|
|
|
Re: Bug in N_PARAMS? [message #64071 is a reply to message #63986] |
Thu, 27 November 2008 13:53   |
Mark[1]
Messages: 66 Registered: February 2008
|
Member |
|
|
On Nov 26, 3:29 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
> Ah. That significantly restricts the utility of
>
> SWITCH N_PARAMS() OF
> 0 : ...
>
> ENDSWITCH
>
> to provide default values for optional parameters.
Indeed. N_ELEMENTS is what you need for that. The only use I have
found for N_PARAMS is for writing wrapper routines
pro wrapper, P1, P2, P3, P4, _REF_EXTRA=_extra
;; Do some other stuff
case n_params() of
0: wrappee, _STRICT_EXTRA=_extra
1: wrappee, P1, _STRICT_EXTRA=_extra
2: wrappee, P1, P2, _STRICT_EXTRA=_extra
3: wrappee, P1, P2, P3, _STRICT_EXTRA=_extra
4: wrappee, P1, P2, P3, P4, _STRICT_EXTRA=_extra
endcase
end
Ugly, but sometimes necessary.
|
|
|
Re: Bug in N_PARAMS? [message #64142 is a reply to message #63974] |
Wed, 03 December 2008 09:06  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Carsten Lechte writes:
>
>> Kenneth P. Bowman wrote:
>>> to provide default values for optional parameters.
>>
>> I wrote this little procedure for setting default values
>> that I use a zillion times a day:
>>
>>
>> PRO defaultarg, arg, defval, VALUEGIVEN=valuegiven
>>
>> IF N_ELEMENTS( arg) EQ 0 THEN BEGIN
>> arg = defval
>> valuegiven = 0
>> ENDIF ELSE valuegiven = 1
>>
>> END
>
> This is a pretty good idea. I can see how it would
> save some typing! :-)
>
> Here is a slightly modified version. I'll add this,
> with documentation, to the Coyote Library later today.
I have a doppelganger at work who takes all my good ideas
as soon as I publish them and points out all their deficiencies
and the obvious ways in which I could have made them better. :-(
To that end, I've updated the SetDefaultValue program I published
earlier to something a good deal simpler that also works in a more
intuitive way. You can find the update, if you are interested in
this sort of thing, here:
http://www.dfanning.com/programs/setdefaultvalue.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|