Re: (newbie) calling a proc with a varying number of arguments
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: (newbie) calling a proc with a varying number of arguments

From: Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Date: Wed Dec 21 2005 - 18:39:01 CET

just80n@gmail.com wrote:
> proc doStuff {arg} {
> domystuff (puts, and lots of things)
> }
[...]
> How can I not get
> called "doStuff" with too many arguments error
> and pack all args into only one ?

If the last formal argument in a [proc] definition is 'args' (not 'arg',
not '&rest', not 'va_args', etc. :-) ) then that variable will receive
not a single argument, but rather a list of *all* remaining arguments.
Take a look at this snippet of interactive session:

   % proc doStuff {args} {
      puts "I have [llength $args] arguments"
   }
   % doStuff a
   I have 1 arguments
   % doStuff "a b c"
   I have 1 arguments
   % doStuff a b c
   I have 3 arguments
   % doStuff to all sorts of things in complicated ways
   I have 8 arguments
   % doStuff
   I have 0 arguments

That should do enough of what you want that you can work the rest out
for yourself. (Remember, use list command and [foreach] when processing
the 'args' variable; make life easy for yourself!)

Donal.
Received on Fri Dec 23 19:02:49 2005