Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Passing-array-values-as-parameters.html

Our email: info@wellho.net • Phone: 01144 1225 708225

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

© 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