| |||||||||||
Array names - variable substitution Posted by neilvp (neilvp), 8 October 2006 Is it possible to have variable substitution when calling an array?For example I am generating a gui that is to display the function and state of a number of registers within a device. My approach to this is to do :- array set register27 { 0 Off On "Link State" 1 Disabled Enabled "Output State" and so on } array set register32 { 0 10 100 "Link Speed" 1 None Errored "Error State" and so on } So that by looking at say bit 0 of register 27 I will get back either a 0 or a 1 which I can then display as The function is [lindex $register27(0) 2] and its state is [lindex $register(0) $bitvalue) (where bit value is either 0 or 1 of course) That works but what I would like to do is loop through all the registers and all their contents and what I can't get to work is : set regnos 27 The function of register $regnos bit $bit is [llindex $register$regnos($bit) 2] The error message is 'can't read 'register' no such variable. i.e it is the $register$regnos($bit) that goes wrong So, how do I use variable substitution in array referencing please... or should I use a different approach? Many thanks Neil Posted by admin (Graham Ellis), 9 October 2006 You're looking to "double parse" the input - in other words, to look for variable names within variabale names. That's not something Tcl normally does - for efficiency's sake, once a variable name has been parsed it's not reparsed just to check there are no more dollars in it. BUT you can force a double parse using set. Here's and example with a simple variable bit it should work just as well with and array:set regno 27 set register27 Mydemo puts [set register$regno] Would I do it that way? No. I would probably have an array called register and elements with names that contain extra parameters: array set register { 27,0 Off On "Link State" 27,1 Disabled Enabled "Output State" and so on 32,0 10 100 "Link Speed" 32,11 None Errored "Error State" and so on } but there may be something I don't know about your application that rukes this out. 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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |