Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Procedure with variable number of arguments

Posted by kinshuk_mishra (kinshuk_mishra), 25 March 2003

I have a query regarding procedure.

The problem:

I want to make a procedure which can telnet to some linux machine and execute set of commands on that machine. The exact number of commands which may be required to execute is not constant and can change as per the requirement.

I now want to know that is there any way in which I can create a procedure that can take variable number of arguments. I know that it is possible as i read about the same in "practical programming in TCL " i could see the example but I think either my edition is very old or i am not able to grasp it. It will be great if some body can help me out with an example.

I am sending my i am only abe to give one $cmd or only fixed number of $cmd's but how can i make this as a variable number....

proc telnet_exec  {ip login password prpasswd cmd} {
        set telnet_prompt  [spawn telnet $ip]
        set cmd1 "echo $?"
        set expPrompt "root@chandra /root#"
        expect "login: "
        set id $spawn_id
        send -i $id "$login\r"
        expect -i $id "Password: "
        send -i $id "$password\r"
        send -i $id "su -\r"
        expect -i $id "Password: "
        send -i $id "$prpasswd\r"
        expect -i $id $expPrompt
        send -i $id "$cmd\r"
        send -i $id "$cmd1\r"
        expect -i $id "0" {
     puts stdout "test case passed"            
        } else {
     puts stdout "test case failed"
        send -i $id "exit"

Posted by admin (Graham Ellis), 25 March 2003
From our training notes - hope this helps

Tcl gives an error and halts if you run a command with the wrong number of parameters. Yet, some commands can take a variable number of parameters.

If you want to write your own command (proc) that takes a variable number of parameters, you can do so. If a parameter is itself a list of two parameters, the first is taken as a variable name and the second as a default value if that parameter is not specified. Thus:

   proc findsize {width {height 0}} {

takes one or two parameters. The first, which must be specified, is placed into the variable width. If a second parameter is specified, it's placed into the variable height. If there is no second parameter, the proc carries on running with height set to 0.

You can also specify a variable, or unlimited, number of parameters on the end of a proc definition by adding the word "args" to the parameter list. All remaining input variables are placed into a list called args if you so specify.


proc ceiling {limit args} {
        set overlimit 0
        foreach item $args {
                if {$item > $limit} {
                        incr overlimit
        return $overlimit }

set test1 [ceiling 20 1 2 4 8 16 32 64 128 256 512]
set test2 [ceiling 15 8 16]

puts "Test 1 - $test1 items over limit"
puts "Test 2 - $test2 items over limit"

And running:

[graham@chapatti folder]$ vardemo
Test 1 - 5 items over limit
Test 2 - 1 items over limit
[graham@chapatti folder]$

Posted by kinshuk_mishra (kinshuk_mishra), 26 March 2003
Thank You very much....This works for me....

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho