Main Page | Report Page

 

  Computers Forum Index » Computer Languages (IDL-PVWAVE) » Procedures with variable/dynamic argument list??...

Author Message
SonicKenking...
Posted: Fri Oct 22, 2010 1:49 am
 
Is it possible in IDL to code a procedure using variable/dynamic
argument list?? i.e. the number of arguments is arbitrary and the
procedure is able to accept them all with some argument array?

Similar to the "va_list" in C or "*args" in Python.

The Print command in IDL is a good example too for accepting arbitrary
number of arguments and print them all. Unfortunately, the Print
command is not written in IDL, hence no sneak peek of the source code
~~
 
Chris Torrence...
Posted: Fri Oct 22, 2010 3:14 pm
 
On Oct 22, 8:26 am, Paolo <pgri... at (no spam) gmail.com> wrote:
Quote:

It comes out pretty ugly indeed... here my "pritn" function
that I use to work around my bad spelling (!)
I guess I didn't know about n_params() when I wrote that.

Ciao,
Paolo

PRO pritn,a1,a2,a3,_extra=_extra

IF exist(a3) THEN print,a1,a2,a3,_extra=_extra ELSE $
   IF exist(a2) THEN print,a1,a2,_extra=_extra ELSE $
      IF exist(a1) THEN print,a1,_extra=_extra

END

;from solarsoft
function exist,var

return,n_elements(var) ne 0

end

Hi all,

You might look at SCOPE_VARFETCH. You could dynamically construct the
argument name, and then pass the string to SCOPE_VARFETCH to retrieve
the value. Something like:

PRO pritn,a1,a2,a3,_extra=_extra
for i=1,n_params() do print, SCOPE_VARFETCH('a'+strtrim(i,
2)),_extra=_extra
end

This only helps in cases where you can break up the arguments and do a
call with each one separately.

Paolo, if I could suggest a tweak, you could use a CASE statement
instead:

case n_params() of
1: print,a1,_extra=_extra
2: print,a1,a2,_extra=_extra
3: print,a1,a2,a3,_extra=_extra
else: message,'Incorrect number of arguments'
endcase

This avoids all of those nasty if/else blocks.

-Chris
ITTVIS
 
Paolo...
Posted: Fri Oct 22, 2010 3:50 pm
 
On Oct 22, 11:14 am, Chris Torrence <gorth... at (no spam) gmail.com> wrote:
Quote:
On Oct 22, 8:26 am, Paolo <pgri... at (no spam) gmail.com> wrote:





It comes out pretty ugly indeed... here my "pritn" function
that I use to work around my bad spelling (!)
I guess I didn't know about n_params() when I wrote that.

Ciao,
Paolo

PRO pritn,a1,a2,a3,_extra=_extra

IF exist(a3) THEN print,a1,a2,a3,_extra=_extra ELSE $
   IF exist(a2) THEN print,a1,a2,_extra=_extra ELSE $
      IF exist(a1) THEN print,a1,_extra=_extra

END

;from solarsoft
function exist,var

return,n_elements(var) ne 0

end

Hi all,

You might look at SCOPE_VARFETCH. You could dynamically construct the
argument name, and then pass the string to SCOPE_VARFETCH to retrieve
the value. Something like:

PRO pritn,a1,a2,a3,_extra=_extra
  for i=1,n_params() do print, SCOPE_VARFETCH('a'+strtrim(i,
2)),_extra=_extra
end

This only helps in cases where you can break up the arguments and do a
call with each one separately.

Paolo, if I could suggest a tweak, you could use a CASE statement
instead:

case n_params() of
1: print,a1,_extra=_extra
2: print,a1,a2,_extra=_extra
3: print,a1,a2,a3,_extra=_extra
else: message,'Incorrect number of arguments'
endcase


Yes, the CASE version is nicer and that's how I would have
wrote it if I had known about n_params() back then... Smile
You always learn something new :)

Thanks,
Paolo

Quote:
This avoids all of those nasty if/else blocks.

-Chris
ITTVIS

 
 
Page 1 of 1    
All times are GMT
The time now is Sun Apr 20, 2014 3:57 pm