Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Double quotes cancels curly braces

61 views
Skip to first unread message

Alexandru

unread,
Feb 14, 2018, 1:54:52 AM2/14/18
to
Above statement seems to be true but unecpected to me. Why?

(bin) 5 % puts {[expr 1+1]}
[expr 1+1]
(bin) 6 % puts "{[expr 1+1]}"
{2}

Thanks.
Alexandru

Harald Oehlmann

unread,
Feb 14, 2018, 2:09:17 AM2/14/18
to
Hello Alexandru,

In the first case, the curly braces (gelockten Klammern) are variable
separators.
In the second case, they are data with no special meaning, as they don't
"embrace a word". The braces only have a special meaning if there is a
space-type character before and after.

See magic Dodekalogue
http://wiki.tcl.tk/10259

Does this makes sense ?

Thank you,
Harald

Alexandru

unread,
Feb 14, 2018, 3:55:10 AM2/14/18
to
So the clou ist that curly braces need white spaces before and after...
Well, it's not making more sense than before since I can't imagine the reason for this rule, but it's enough as an explanation. I mean, normally, the double quotes never changed the meaning of what's inside them. Everything is evaluated as it is between double quotes.

Cecil Westerhof

unread,
Feb 14, 2018, 3:59:05 AM2/14/18
to
Alexandru <alexandr...@meshparts.de> writes:

> Above statement seems to be true but unecpected to me. Why?
>
> (bin) 5 % puts {[expr 1+1]}
> [expr 1+1]
> (bin) 6 % puts "{[expr 1+1]}"
> {2}

Because things between double quotes are evaluated. For example you
can do something like:
puts "There are ${count} entries"

And you expect output like:
There are 13 entries

When you do not want expansion you use the curly braces.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

stefan

unread,
Feb 14, 2018, 4:11:24 AM2/14/18
to
> I can't imagine the reason for this rule, but it's enough as an explanation.

Double quotes take precedence over any other special-purpose characters. See https://www.tcl.tk/man/tcl/TclCmd/Tcl.htm#M8. This comes handy in exercises of meta-programming. One can so string-assemble a script containing curly braces, for instance.

Rich

unread,
Feb 14, 2018, 6:59:02 AM2/14/18
to
Alexandru <alexandr...@meshparts.de> wrote:
> Am Mittwoch, 14. Februar 2018 08:09:17 UTC+1 schrieb Harald Oehlmann:
>> Am 14.02.2018 um 07:54 schrieb Alexandru:
>> > Above statement seems to be true but unecpected to me. Why?
>> >
>> > (bin) 5 % puts {[expr 1+1]}
>> > [expr 1+1]
>> > (bin) 6 % puts "{[expr 1+1]}"
>> > {2}
>> >
>> > Thanks.
>> > Alexandru
>> >
>> Hello Alexandru,
>>
>> In the first case, the curly braces (gelockten Klammern) are variable
>> separators.
>> In the second case, they are data with no special meaning, as they don't
>> "embrace a word". The braces only have a special meaning if there is a
>> space-type character before and after.
>>
>> See magic Dodekalogue
>> http://wiki.tcl.tk/10259
>>
>> Does this makes sense ?
>>
>> Thank you,
>> Harald
>
> So the clou ist that curly braces need white spaces before and after...

No, the requirement is stated in the "Tcl" manpage:

[6] Braces.

If the first character of a word is an open brace ("{") and rule
[5] does not apply,

Where 'word' is defined by rule 3 in the same man page:

[3] Words.
Words of a command are separated by white space (except
for newlines, which are command separators).

As well, rule 5 is the {*} rule.

So, in order for an open brace to be handled as a brace, it must be the
first character at the start of a word. Only then does it take on its
"brace" meaning.

In your example with the double quotes, the "word" begins with a double
quote, so rule 6 does not apply.

> Well, it's not making more sense than before since I can't imagine
> the reason for this rule, but it's enough as an explanation. I mean,
> normally, the double quotes never changed the meaning of what's
> inside them. Everything is evaluated as it is between double quotes.

The quote rule is:

[4] Double quotes.
If the first character of a word is double-quote (""") then the
word is terminated by the next double-quote character. If
semi-colons, close brackets, or white space characters (including
newlines) appear between the quotes then they are treated as
ordinary characters and included in the word. Command
substitution, variable substitution, and backslash substitution are
performed on the characters between the quotes as described below.
The double-quotes are not retained as part of the word.

So in your double quote example, your 'word' began with double quote,
so rule 4 applied, and rule 6 (braces) was never considered by the
parser.

And, in your example, the double quote rule worked as defined, because
"command substitution" occurred (the [expr 1+1] command substitution
was performed).

Gerald Lester

unread,
Feb 14, 2018, 8:21:54 AM2/14/18
to
NO!!!!!!

You need to read, understand and *BELIEVE* exactly what the Dodekalogue
(http://wiki.tcl.tk/10259) says (and not what you think because of
mental baggage from other languages) or you will get in trouble with Tcl!!!

The long and short are that both double quotes and curly braces are
types of QUOTATIONS -- and this is all they are!

Curly braces do *NOT* define a constant list vs a constant string.

Curly braces do *NOT* define a block of code.

The following is totally legal and valid:

proc getElement "list elementNumber" "\
return \[lindex \$list \$elementNumber\]
"

set myList "This is a list of words"
puts [getElement $myList 4]

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+
0 new messages