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.
Passing array values as parameters

Posted by neo (The Architect), 20 May 2005
How can I pass array values as parameters to a function.

For e.g. I have an array IP address and depending on a command line arg (say a mean I want to telnet to a certain host using expect. How do i do this?

I tried passing the array val using $ARRAY($ARG) but this gives me an error saying
can't read "var1": no such variable

where var1 is the argument for $ARRAY($ARG) in the proc

Posted by admin (Graham Ellis), 21 May 2005
Here's an example of passing an array member into a function.

proc reporton thisone {
      puts "Learning about $thisone"

set ARG archirecture

set ARRAY(plumbing) "Fluid Dynamics"
set ARRAY(archirecture) "The Tcl Language"
set ARRAY(townplanning) "Building Studies"

reporton $ARRAY($ARG)

Which gives these results

earth-wind-and-fire:~/may05 grahamellis$ tclsh tdo
Learning about The Tcl Language
earth-wind-and-fire:~/may05 grahamellis$

Can you supply me with a similar sample piece of code that shows the problem you're having ... I don't think its directly to do with passing from the array - it's some other problem perhaps.

As a footnote - if you need to pass a complete array into a proc, you should call be name and use the upvar command. There are lots of examples of upvar - a feature that people always take a while to grasp - at

Posted by neo (The Architect), 21 May 2005
proc login2host userName userPass prompt {
       expect -re ".*login: "
       send "$userName1\r"
       expect -re "Password: "
       send "$userPass\r"
       expect -re "$prompt"
       send "set terminal telnet idle=0\r"
       expect -re "$prompt"

#I call it here like this

Posted by neo (The Architect), 21 May 2005
Ok i think I got it the args should be passed in parentheses  
I had done this but I had separated them with commas and that did not work.

Posted by admin (Graham Ellis), 21 May 2005
The proc command takes three parameters and yours has five. I suspect your code should read

proc login2host {userName userPass prompt} {

and not

proc login2host userName userPass prompt {

(note the extra curly braces that I've added)

OK - we both posted at the same time. Glad it sorted anyway

Posted by neo (The Architect), 21 May 2005
Yeah I changed that and it works fine now.  
I have another doubt though.

I would like to execute a list of commands and receive expected responses.  Among an array or a list which one would be better/easier?

Posted by admin (Graham Ellis), 21 May 2005
In general "lists v arrays" ... you would choose a list if you want the elements to be in order (as a list is an ordered collection) and an array if you want to be able to select them quickly by key (as an array in Tcl is an unordered collection)

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