Home > Archive > Tcl > July 2005 > Quoted braces?!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I'm sure this has been asked before, but I've not been able to find it.
I have always treated braces such that their contents are not evaluated.
However, someone recently posed this question to me, and I can't figure
out why it works! I'm using TCL 8.4.5 Linux and ActiveTCL 8.5a2, the
other person's using ActiveTCL 8.4.10 Windows, though I suspect it's
something more general that I've avoided learning so far.
Given the brief code:
set var1 1
set var2 2
puts "{$var1 $var2}"
=====
{1 2}
I expected to see {$var1 $var2}, but instead it seems to be evaluated
to {1 2}. If I put the braces around the whole, of course, it's not
evaluated (i.e. {"{$var1 $var2}"} => "{$var1 $var2}", as I expected).
I've also tried this in a proc, and it's not (as was my first
impression) that puts is evaluating $var1 and $var2:
proc x {} {
set var1 1
set var2 2
return "{$var1 $var2}"
}
set var1 4
set var2 5
puts [x]
=====
{1 2}
Am I just missing something obvious, or is this some aspect of quoted
braces that I don't quite get yet? ;)
Thanks in advance,
-TC
--
swap triangle and cannedmeat to reply
| |
| Bryan Oakley 2005-07-24, 8:59 pm |
| Tim C wrote:
>
> Given the brief code:
> set var1 1
> set var2 2
> puts "{$var1 $var2}"
> =====
> {1 2}
>
> I expected to see {$var1 $var2}, but instead it seems to be evaluated
> to {1 2}. If I put the braces around the whole, of course, it's not
> evaluated (i.e. {"{$var1 $var2}"} => "{$var1 $var2}", as I expected).
Braces only have special significance if used for quoting. If they are
part of some data they have no special meaning. Thank goodness!
In your example, {}'s are not used for quoting, they are part of the
data that is quoted with double quotes. Since they aren't used for
quoting they have no special behavior.
| |
| SM Ryan 2005-07-24, 8:59 pm |
| # I've also tried this in a proc, and it's not (as was my first
# impression) that puts is evaluating $var1 and $var2:
# proc x {} {
# set var1 1
# set var2 2
# return "{$var1 $var2}"
# }
# set var1 4
# set var2 5
# puts [x]
# =====
# {1 2}
#
# Am I just missing something obvious, or is this some aspect of quoted
# braces that I don't quite get yet? ;)
Your entire proc body is a {-}-quoted string. If the proc body is a
"-quoted string or a [list ....] or something else, you'll get different
results.
% proc x {} {
set var1 1
set var2 2
return "{$var1 $var2}"
}
% set var1 4
4
% set var2 5
5
% puts [x]
{1 2}
% proc y {} "
set var1 1
set var2 2
return \"{$var1 $var2}\"
"
% puts [y]
{4 5}
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
|
|
|
|
|