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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Extracting list args passed to a procedure

Posted by Nuts (Nuts), 9 October 2007
Hi,
     I am passing a list (containing strings) to  procedure. Now, if i try
to read the individual elements of the '"args" argument in the procedure I couldn't get the individual value of the strings passed.

## File1 :  test.tcl
## To connect to the NE
set cmdtype TL1
set ipaddr  192.168.200.1
set portaddr 10001
set params [list $cmdtype $ipaddr $portaddr ]
connect $params


## File2 : connect.tcl
proc connect {args} {

  if {[llength $args] < 1} {
       error "not enough args"
   }

  puts "Value of args : $args\n"

  set cmdtype [lindex $args 0]
  set ipaddr [lindex $args 1]
  set portaddr [lindex $args 2]

   puts "Value of cmdtype : $cmdtype"

if {$cmdtype == "TL1"} {
       puts "Connecting TL1 interface..."


### Output ###
[neetesh@neetesh-OME expect_sample]$ ./test.tcl
Value of args : {TL1 192.168.200.1 10001}

Inside connect procedure........

Value of cmdtype : TL1 192.168.200.1 10001
------------------------------------------------------------------------------------------

Kindly help me in this.

regards,
Neetesh




Posted by admin (Graham Ellis), 9 October 2007
You're only passing a single list across, so there's only one thing in that list in the proc.   You need to look at the list within the list.   Here's an example of what I mean:

Code:
proc connect {args} {
       foreach thing $args {
               puts $thing
               foreach within $thing {
                       puts $within
               }
       }
}

set params [list tomato apple banana]
connect $params


And that runs as follows

Code:
earth-wind-and-fire:~/oct07 grahamellis$ tclsh ttc
tomato apple banana
tomato
apple
banana
earth-wind-and-fire:~/oct07


For future note, if you can trim down you questions to fewer lines, it would be a huge help.  Thanks!

Posted by Nuts (Nuts), 10 October 2007
Thanks Graham !! It worked for me.

regards,
Nuts



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