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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
Using an array value as another array name

Posted by shubb (shubb), 6 January 2006
Hi, I'm trying to use the values from one array as names of other arrays for excecuting commands.

I create several data arrays with values.
set testArr1('name') "test1"
set testArr1('val1') "value11"

set testArr2('name') "test2"
set testArr2('val1') "value21"

Then I create another array with the names of each data array.
set arrayNames(0) "testArr1"
set arrayNames(1) "testArr2"

I want to use the arrayNames in a foreach loop to get the name of each of the data arrays, so I can use the data in those arrays for commands.  The problem is that when I use the foreach loop, it reads the names correctly, but it doesnt use the value to look up the data array by the same name.

foreach arrData [array names arrayNames] {
    send_user "data array's name is $arrayNames($arrData)('name')\n"

What this returns is:
data array's name is testArr1('name')
data array's name is testArr2('name')

when what I want to get is the value of testArr1('name'), which is "test1" and "test2"

Any Idea how I to do this?
As you can probably guess, I'm a beginner with Expect.
Is there a better way to accomplish this?

Thanks for any help you can offer.

Posted by admin (Graham Ellis), 7 January 2006
I would tackle it rather differently - the way you try to store one name in another is a bit obtuse and you'll do better with just one array:

set abc(def,name) hello
set abc(def,value) "a way to greet people"
set abc(ghi,name) goodbye
set abc(ghi,value) "a way to conclude a conversation"

foreach gname [array names abc *,name] {
       set subname [lindex [split $gname ,] 0]
       puts "$abc($subname,name) IS $abc($subname,value)"


earth-wind-and-fire:~/jan06 grahamellis$ expect eee
hello IS a way to greet people
goodbye IS a way to conclude a conversation
earth-wind-and-fire:~/jan06 grahamellis$

Posted by shubb (shubb), 8 January 2006
Awesome!  That works perfect.

Thanks!  I knew there had to be a better way to do that.

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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho