This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Its very hard to reply reasonably without some background of the correspondent on their Wiki bio page. Thanks,gold
gold12Dec2018. 3/26/2024 Updated. Here are some practical rules on curly braces and quotes in TCL. Per question of “ What is the difference between double quotes (" ") and Braces { } ? “ from Kh_Yogananda_Raj_Urs. More complete references etc are listed below. Thanks for feedback. Maybe, my head is turned around in Etcl from Unix. Per your feedback and implied suggestions, have reduced discussion and examples here in new update, using no list commands from out of this context.
gold 3/26/2024 Updated. TCL, or Tool Command Language, is a powerful scripting language that is widely used in various applications. Two of the most commonly used symbols in TCL are double quotes and curly braces, which serve distinct purposes. Let’s explore the differences and usage of these symbols.
Double Quotes (" "): Double quotes allow interpolation to occur within the group. This means that when you enclose text in double quotes, TCL will substitute variables, commands, and backslashes within the quoted string. For example:
# TCL set myvar "Hello, world!" puts "The value of myvar is: $myvar"
In this case, the variable $myvar is interpolated within the double-quoted string, resulting in the output: “The value of myvar is: Hello, world!”
Curly Braces ({ }): Curly braces are used to group words (and lines) without interpolation. They act similarly to single quotes in shell scripting. When you enclose text in curly braces, TCL treats it as a literal string, without any variable substitution. For example:
#TCL set myvar "Hello, world!" puts {The value of myvar is: $myvar}
In this case, the curly braces prevent variable interpolation, and the output will be: “The value of myvar is: $myvar”. Proc Definitions: When defining a procedure (proc), curly braces are commonly used to pass a script as an argument without interpolating it. This ensures that the script inside the proc definition remains unaltered. For example:
# TCL Example proc add3 {a b c} { return [expr {$a + $b + $c}] } proc add4 {a b c} {[expr {$a + $b + $c}]} # Alternative usage without expressed return statement included here.
Remember that curly braces are primarily for quoting, and there’s no strict requirement to use them. TCL’s flexibility allows you to choose whether or not to employ curly braces based on your specific needs.
Proc Definitions: When defining a procedure (proc), curly braces are commonly used to pass a script as an argument without interpolating it. The curly braces ensure that the script inside the proc definition remains unaltered.
# TCL Example: proc add3 {a b c} {return [expr {$a + $b + $c}]} proc add4 {a b c} {[expr {$a + $b + $c}]} # Alternative usage without expressed return statement included here.
Remember that curly braces are primarily for quoting, and there’s no strict requirement to use them. Tcl’s flexibility allows you to choose whether or not to employ curly braces based on your specific needs.
In summary, double quotes in TCL are used to create strings while curly braces are used for grouping command words together. Double quotes allow for variable interpolation and special character interpretation, while curly braces do not. Understanding the difference between these symbols is crucial for effective programming in TCL.
AMG: 2018? A note concerning the [list] command: It's not necessary to use [list] if none of the elements are the product of substitution. In other words, if the list will always have the same elements, you don't need the [list] command to construct the list. Instead simply surround the list with {braces}. Its internal representation will become list when it gets used as a list, since Tcl has duck typing. Similar goes for [dict create]. I mean, when you want a number, you just type the number, even though its initial internal representation will be string. It "becomes" a number when context shows that it is a number. You never see code that does this: set var [expr 5]; only this: set var 5.
HE 2021-09-11: You wrote in the second paragraph of chapter 'Introduction': "... with list command like set lister1 { dog cat lizard } to store separate items in a list". This line of code not use any list command.
set lister1 { dog cat lizard }
will store the string inside the curly braces as the string representation of the Tcl Object. A possible internal list representation will only be created in case a command is called which needs the list representation. That this should be a list is only in the mind of the programmer. Tcl doesn't know at that stage, that this should be a list. Because no command is used which needs the list representation, { dog cat lizard } stays as a string until the end of the example code.
If you would have used, for example:
puts [lindex $lister1 1]
the list representation would be created.
This is the same with the lister2 and lister3 examples at the point in time of writing this comment.
For example to show that lister3 becomes a list you should have to use:
set lister3 {" dog cat” lizard } puts [lindex $lister3 0]
By the way, the ';' at the end of a Tcl command is only needed in case you want to write another command on the same line.
gold Update 3/26/2024 . Thanks for feedback. Maybe my head is turned around. Per your implied suggestion, have reduced discussion and examples here in new update.
AMG: A note concerning the [list] command: It's not necessary to use [list] if none of the elements are the product of substitution. In other words, if the list will always have the same elements, you don't need the [list] command to construct the list. Instead simply surround the list with {braces}. Its internal representation will become list when it gets used as a list, since Tcl has duck typing. Similar goes for [dict create]. I mean, when you want a number, you just type the number, even though its initial internal representation will be string. It "becomes" a number when context shows that it is a number. You never see code that does this: set var [expr 5]; only this: set var 5.
Please place any comments here with your wiki MONIKER and date, Thanksgold12Dec2018
HE 2021-09-11: You wrote in the second paragraph of chapter 'Introduction': "... with list command like set lister1 { dog cat lizard } to store separate items in a list". This line of code not use any list command.
set lister1 { dog cat lizard }
will store the string inside the curly braces as the string representation of the Tcl Object. A possible internal list representation will only be created in case a command is called which needs the list representation. That this should be a list is only in the mind of the programmer. Tcl doesn't know at that stage, that this should be a list. Because no command is used which needs the list representation, { dog cat lizard } stays as a string until the end of the example code.
If you would have used, for example:
puts [lindex $lister1 1]
the list representation would be created.
This is the same with the lister2 and lister3 examples at the point in time of writing this comment.
For example to show that lister3 becomes a list you should have to use:
set lister3 {" dog cat” lizard } puts [lindex $lister3 0]
By the way, the ';' at the end of a Tcl command is only needed in case you want to write another command on the same line.
gold 9/27/2021. Switched some comment signs ;# to #. This a big file. Check earlier editions, if not compatible. Maybe obvious, but this page was written on Windows10 Tcl ports including ActiveTCL. I assume reader can cut and paste on screen, what the reader needs, and tootle on to his own project and own contribution pages to the TCL Wiki.