parray foo foo("bar") = 12 >set foo(bar) 12 12 >parray foo foo("bar") = 12 ...">

Q & A tcl - quotation mark key

it surprised me

>set foo("bar") 12 12 >parray foo foo("bar") = 12 >set foo(bar) 12 12 >parray foo foo("bar") = 12 foo(bar) = 12 

It seems that literal foo is not the same as "foo". But still

 >string length foo 3 >string length "foo" 3 

What? I do not understand

+4
source share
1 answer

The character " is special for the Tcl parser at the beginning of a word (or, of course, a word starting with " ). In fact, if you put spaces, you would get the error:

 % set foo("bar") 2 wrong # args: should be "set varName ?newValue?" 

In the case when you make calls to string length , " is at the beginning of the word, so it is special. If we add an extra start character, we will see that the feature " disappears:

 % string length x"bar" 6 

If you are doing something complicated with array indexes, I think it’s usually easier to put the element name in a variable, since then it becomes clearer what is happening (and it is usually easier to debug):

 set idx "bar" set foo($idx) 12 
+8
source

Source: https://habr.com/ru/post/1337545/


All Articles