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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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 10.10.10.10) 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
Quote:
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.

Code:
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

Code:
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 http://www.wellho.net/resources/T207.html.

Posted by neo (The Architect), 21 May 2005
Code:
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
login2host $USERNAME($SHS_NO) $USERPASS($SHS_NO) $PROMPT($SHS_NO)


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 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