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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Procedure with variable number of arguments

Posted by kinshuk_mishra (kinshuk_mishra), 25 March 2003
Hi,

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 procedure....here 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.

Code:
#!/usr/bin/tcl

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 www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho