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

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

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

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:

Code:
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)"
       }


runs

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