Michael Wallace wrote:
> How do you define a procedure to take N number of arguments when you
> don't know what N is before the procedure call? For those of you who
> have worked with C, what I'm after is something similar to the ellipsis
> (...) which allows N many arguments to be specified for functions such
> as printf.
>
> In IDL, the print command is obvious example of what I'm trying to do.
> The signature of print is:
>
> print [, Expr1, Expr2, ... , ExprN]
>
> So, how can I write a procedure to take N many arguments?
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.
For example, one of my general-purpose routines (MGH_NEW, a procedure
wrapper for OBJ_NEW) looks like this (simplified):
pro MGH_NEW, name, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, $
RESULT=result
if size(name, /TYPE) ne 7 then $
message, 'The first parameter must be a class name.'
case n_params() of
1: result = obj_new(Name)
2: result = obj_new(Name, P1)
3: result = obj_new(Name, P1, P2)
4: result = obj_new(Name, P1, P2, P3)
5: result = obj_new(Name, P1, P2, P3, P4)
6: result = obj_new(Name, P1, P2, P3, P4, P5)
7: result = obj_new(Name, P1, P2, P3, P4, P5, P6)
8: result = obj_new(Name, P1, P2, P3, P4, P5, P6, P7)
9: result = obj_new(Name, P1, P2, P3, P4, P5, P6, P7, P8)
10: result = obj_new(Name, P1, P2, P3, P4, P5, P6, P7, $
P8, P9)
11: result = obj_new(Name, P1, P2, P3, P4, P5, P6, P7, $
P8, P9, P10)
else: message, 'Too many parameters'
endcase
end
It used to take 15 positional parameters, but I trimmed it down to 10,
which is still more than enough. I don't know the maximum number
supported by IDL, but it's way more than you would want to use in normal
circumstances.
Inquiring about the number and status of your parameters is a subtle
business. I'm sure David has some useful tutorials.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|