Version 44 of Practical_Advice_on_Quotes_and_Brackets_in_TCL

Updated 2024-03-26 17:49:44 by gold

Practical_Advice_on_Quotes_and_Braces_in_TCL

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


Title: Practical_Advice_on_Quotes_and_Braces_in_TCL

Preface

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.


Title: The Difference and Usage of Double Quotes and Curly Braces in TCL


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.


Summary


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.




References:




Good Feed Back from AMG



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.



Good Feed Back from HE


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.



Hidden Comments Section

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.



Introduction


Here are some practical rules on curly braces and quotes in TCL. curly braces are essential to speed up math expressions in expr. Quotes are mainly used for exact text strings with some variable substitution. For example, puts " result value is expr { 1+3} " and print out is < result value is 4 >. If you are using the TCL console or easy eye calculator in the file system, paste in the TCL statement and this should show in big fonts on the console window. From the inside the double quotes the text is output exactly, whereas math expressions and $variables are evaluated inside the quotes. For practical use on the same line of code, quotes in TCL can printout exact text, text titles of variables, evaluated expressions, and $variables stored in programs.


Aside from expr, brackets are used with list command like set lister1 [ list dog cat lizard ] to store separate items in a list. Sometimes, curly braces are used with set command like set lister2 { list dog cat lizard } to store combined items in a string. The statement <set lister2 {" dog cat lizard" }; would store the three items in a single string representing one item in the list. Paste these expressions into the console and evaluate them . Here is a little console program: set lister1 { dog cat lizard } ; puts $lister1; set lister2 {" dog cat lizard" };puts $lister2;set lister3 {" dog cat” lizard };puts $lister3;set lister4 " dog cat lizard" ;$lister4; The TCL interpreter will see <set lister1 { dog cat lizard}> as three text variables stored in a single list. Quotes can act as a basket, mold, or glue to store text phrases inside the interpreter (in list structure). The TCL interpreter will see <set lister3 {" dog cat” lizard }> as two text variables of " dog cat” and “lizard” in a list. The TCL interpreter will see <set lister3 {" dog cat lizard"}> as one text variable in a list. The TCL interpreter will see <set lister4 " dog cat lizard"> as one text variable in a single string of text characters. The gist is that quotes and curly braces are used to tell the TCL interpreter how to evaluate the enclosed items either in parts or whole string, and much differently in some usage. Part of the confusion is that <dog cat lizard> can either be treated as either one item, two items, or three items in the interpreter, but the simple print out on the TCL console looks the same regardless. --- The key concept is how the items and information are stored and typed in the TCL interpreter either as a text string or list. The interpreter will usually try to type or tag the loaded data immediately. For an analogy in reading, the human brain loads the items above and tags them with associations, like fur for “dog cat”,scales for “lizard”, and teeth & claws for all. But the treatment of <dog cat lizard> in the mental processes generally depends on the use and association (symbols) for each individual item.


Conclusions


The key concept is how the items and information are stored and typed in the TCL interpreter either as a text string or list. The interpreter will usually try to type or tag the loaded data immediately. For an analogy in reading, the human brain loads the items above and tags them with associations, like fur for “dog cat”,scales for “lizard”, and teeth & claws for all. But the treatment of <dog cat lizard> in the mental processes generally depends on the use and association (symbols) for each individual item.


write puts "[lindex $lister1 0 ]" returns dog and puts "[lindex $lister1 1 ]" cat. If lindex is working correctly, does that mean a list has been created? Have chew on this. Maybe lindex works on both lists and strings. Is the better command set lister2 [ list dog cat lizard ] ? Quoting reference [L1 ] " In fact, a command is free to interpret its arguments as any sort of thing, e.g., a string, list, integer, float, etc. The built-in list commands can take a string and interpret it as a list, as shown in this slightly modified example from shimmering : set x {a b c}". Per your suggestion, have added command, set lister1 [ list dog cat lizard ] to introduction.


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.