| Re: Ellipsis in IDL? [message #40113 is a reply to message #40103] |
Wed, 21 July 2004 21:58   |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Michael Wallace wrote:
>> You can't do exactly that, but you can write a routine to accept a
>> large number of arguments (better called positional parameters) and
>> then use the various inquiry functions (like N_PARAMS, SIZE,
>> N_ELEMENTS & ARG_PRESENT) in your code to handle the different cases.
>
>
> Ah, I see. I think I can work with this. I don't expect to ever have
> more than eight or so argu-- positional parameters. I have used SIZE
> and N_ELEMENTS in the past, but I had never run across N_PARAMS until
> now. That greatly simplifies things. It's a good thing you mentioned
> it or I probably would have written my own. ;-) I say that because one
> time I got part of the way through writing my own KEYWORD_SET type of
> function before realizing that IDL already had it's own handy
> KEYWORD_SET function.
Actually, I don't expect you'll want to use N_PARAMS all that much. All
it tells you is how many positional parameters there are. So it's useful
when you want to pass these parameters on to another routine, without
knowing *anything* about them, as in the example I included. More
usually, you'll want to know if each parameter is currently associated
with data; you can use N_ELEMENTS for that, eg:
function add, p0, p1, p2, p3
if n_elements(p0) eq 0 then message, 'Nothing to add'
result = p0
if n_elements(p1) gt 0 then result = result + p1
if n_elements(p2) gt 0 then result = result + p2
if n_elements(p3) gt 0 then result = result + p3
return, result
end
Sometimes you'll want to know if changes you make to parameter data will
be retained when you exit. If not you may be able to save time by not
computing it. You can use ARG_PRESENT for that, eg
pro set, p0, p1, p2, p3
if arg_present(p0) then p0 = <expensive operation>
if arg_present(p1) then p1 = <expensive operation>
if arg_present(p2) then p2 = <expensive operation>
if arg_present(p2) then p3 = <expensive operation>
end
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
|