comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Procedures with variable/dynamic argument list??
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Procedures with variable/dynamic argument list?? [message #73027] Mon, 25 October 2010 06:51
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 10/25/10 1:32 am, SonicKenking wrote:
> Thanks all for the reply.
> It is good to be clear that it is not directly possible to make a
> dynamic argument list. I'll just go with the dummy parameter way and
> use scope_varfetch to loop on the actual passed arguments.

You can do it from within a DLM routine, though.

Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
Re: Procedures with variable/dynamic argument list?? [message #73037 is a reply to message #73027] Mon, 25 October 2010 00:32 Go to previous message
SonicKenking is currently offline  SonicKenking
Messages: 51
Registered: October 2010
Member
Thanks all for the reply.
It is good to be clear that it is not directly possible to make a
dynamic argument list. I'll just go with the dummy parameter way and
use scope_varfetch to loop on the actual passed arguments.
Re: Procedures with variable/dynamic argument list?? [message #73075 is a reply to message #73037] Fri, 22 October 2010 08:50 Go to previous message
pgrigis is currently offline  pgrigis
Messages: 436
Registered: September 2007
Senior Member
On Oct 22, 11:14 am, Chris Torrence <gorth...@gmail.com> wrote:
> On Oct 22, 8:26 am, Paolo <pgri...@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... :)
You always learn something new :)

Thanks,
Paolo

> This avoids all of those nasty if/else blocks.
>
> -Chris
> ITTVIS
Re: Procedures with variable/dynamic argument list?? [message #73077 is a reply to message #73075] Fri, 22 October 2010 08:14 Go to previous message
chris_torrence@NOSPAM is currently offline  chris_torrence@NOSPAM
Messages: 528
Registered: March 2007
Senior Member
On Oct 22, 8:26 am, Paolo <pgri...@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

This avoids all of those nasty if/else blocks.

-Chris
ITTVIS
Re: Procedures with variable/dynamic argument list?? [message #73084 is a reply to message #73077] Fri, 22 October 2010 07:26 Go to previous message
pgrigis is currently offline  pgrigis
Messages: 436
Registered: September 2007
Senior Member
On Oct 22, 9:56 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
> In article
> < d5d024b5-fb3e-438e-9277-c4dc032c3...@n24g2000prj.googlegroup s.com >,
>
>  SonicKenking <ywa...@gmail.com> wrote:
>> 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
>> ~~
>
> IDL procedures and functions are flexible about how many
> arguments you provide, as long as you don't pass too many.
>
> You do have to provide at least as many dummy arguments
> in the procedure definition as you intend to use.  That is
>
>    PRO VAR_ARGS, a1, a2, a3, a4
>
> can be called as
>
>    VAR_ARGS, x, y
>
> It is up to the procedure to know what to do in that
> case.  (See the N_PARAMS function.)
>
> As far as I know, there is no direct mechanism for
> handling an arbitrary number of arguments other than
> making a list in the procedure/function definition.
>
> Ken

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
Re: Procedures with variable/dynamic argument list?? [message #73089 is a reply to message #73084] Fri, 22 October 2010 06:56 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article
<d5d024b5-fb3e-438e-9277-c4dc032c35e2@n24g2000prj.googlegroups.com>,
SonicKenking <ywangd@gmail.com> wrote:

> 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
> ~~

IDL procedures and functions are flexible about how many
arguments you provide, as long as you don't pass too many.

You do have to provide at least as many dummy arguments
in the procedure definition as you intend to use. That is

PRO VAR_ARGS, a1, a2, a3, a4

can be called as

VAR_ARGS, x, y

It is up to the procedure to know what to do in that
case. (See the N_PARAMS function.)

As far as I know, there is no direct mechanism for
handling an arbitrary number of arguments other than
making a list in the procedure/function definition.

Ken
Re: Procedures with variable/dynamic argument list?? [message #73102 is a reply to message #73089] Fri, 22 October 2010 02:04 Go to previous message
SonicKenking is currently offline  SonicKenking
Messages: 51
Registered: October 2010
Member
On Oct 22, 7:57 pm, Gray <grayliketheco...@gmail.com> wrote:
> On Oct 21, 9:49 pm, SonicKenking <ywa...@gmail.com> wrote:
>
>> 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
>> ~~
>
> You want the _EXTRA keyword.

hmm, correct me if I am wrong. But _EXTRA is only good for accepting
extra keyword arguments, not positional arguments. I need something
similar to _Extra, but accept positional arguments.
Re: Procedures with variable/dynamic argument list?? [message #73103 is a reply to message #73102] Fri, 22 October 2010 01:57 Go to previous message
Gray is currently offline  Gray
Messages: 253
Registered: February 2010
Senior Member
On Oct 21, 9:49 pm, SonicKenking <ywa...@gmail.com> wrote:
> 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
> ~~

You want the _EXTRA keyword.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDLdoc 3.3 released
Next Topic: Re: the "real" screen size

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:28:55 PDT 2025

Total time taken to generate the page: 0.00810 seconds