I have the following problem:
set start "input[0]"
set end "output[0]"
set myArray($start,$end,pin) 1
set x "input[0]"
set y "output[0]"
set test [array names myArray $x,$y,pin]
puts "\n$test"
output should be:
input[0],output[0]
but output is:
{}
if I do:
set test [array names myArray *,*,pin]
puts "\n$test"
output is:
input[0],output[0]
Also, if I replace []
by {}
ie.input{0},output{0}
original code works.
Can someone please tell me what is happening here? How do I escape the []
brackets?
Well, let’s look what is going on here:
set start “input[0]”
This will execute the command 0. I don’t know what that does, but you propably don’t want that. Either escape brackets with \ or use {} to enclose the name.
Same applies for
set end “output[0]”
# …
set x “input[0]”
set y “output[0]”
But it doesn’t look like you have that problem.
The next thing is that you pass a glob pattern to array names:
set test [array names myArray $x,$y,pin]
Tcl uses [] in glob patterns for character choices, simmilar to regexp. So your pattern will only match input0,output0,pin. You can avoid that by passing the -exact switch to array names:
set test [array names myArray -exact $x,$y,pin]
Or you can escape the brackets again with a backslash (note that the pattern should look like input\[0\],input\[1\],pin after Tcl did all kinds of substitution.)