Unbalanced braces in variable names 
Author Message
 Unbalanced braces in variable names

I accidentally came across this. Using Tcl 8.4.4 on Linux I wrote

  % set array(id 3

where I really meant

  % set array(id) 3

Tcl accepted that and made a non-array variable named 'array(id'. So I
thought the brace must be legal character for variable names. But if I
really used the stupid idea of naming a string like 'string(1' I
cannot do

  % set string(1 "a string"
  % string length $string(1

Giving this command in tclsh will not work, the command will not
finish. But

  % string length ${string(1}

will give the correct result. I feel this is inconsistent. Or am I
missing a point here?

Torsten



Fri, 31 Mar 2006 16:10:03 GMT  
 Unbalanced braces in variable names

Quote:

> I accidentally came across this. Using Tcl 8.4.4 on Linux I wrote

>   % set array(id 3

> where I really meant

>   % set array(id) 3

> Tcl accepted that and made a non-array variable named 'array(id'. So I
> thought the brace must be legal character for variable names. But if I
> really used the stupid idea of naming a string like 'string(1' I
> cannot do

>   % set string(1 "a string"
>   % string length $string(1

> Giving this command in tclsh will not work, the command will not
> finish. But

>   % string length ${string(1}

> will give the correct result. I feel this is inconsistent. Or am I
> missing a point here?

Not much, only the point that $ behaves different then set..., look up
man Tcl, its documented behaviour. See point [7] variable substitution
and note the difference between $var and ${var} .

Michael



Fri, 31 Mar 2006 19:47:20 GMT  
 Unbalanced braces in variable names

Quote:

> Tcl accepted that and made a non-array variable named 'array(id'. So I
> thought the brace must be legal character for variable names. But if I
> really used the stupid idea of naming a string like 'string(1' I
> cannot do

I can't answer your question, but to avoid confusion, we call those
things parentheses, not braces.


Fri, 31 Mar 2006 20:47:48 GMT  
 Unbalanced braces in variable names
#   % set string(1 "a string"
#   % string length $string(1
#
# Giving this command in tclsh will not work, the command will not
# finish. But
#
#   % string length ${string(1}

set knows its second argument is the variable name, and so it figures out some
way to parse it. $ knows that a variable name can follow, and tries to find the
longest prefix that is valid variable. ${...} again knows the entire extent
of the variable name and figures out a way to parse it.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
The whole world's against us.



Sat, 01 Apr 2006 22:27:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. passing a prolog string to tcl and unbalanced braces

2. unbalanced braces when passing a prolog string to tcl

3. Using a variable name for a variable name??

4. Brackets in brackets, braces in braces

5. Brackets in brackets, braces in braces

6. TCL evaluation curly brace that is part of my variable value

7. Annoying bracing of variables

8. field name and key name as a variable

9. field name and key name as a variable

10. variable contents to variable name

11. array name in variable variable

12. getting variable name from the variable itself?

 

 
Powered by phpBB® Forum Software