Practical_Advice_on_Quotes_and_Brackets_in_TCL

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. 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.


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.


References:



Appendix Code

Trial Console Program

        # TCL source code follows
        # ASED autoindent applied
        # Trial Console Program  brackets and quotes.
        # written on Windows XP on TCL
        # working under TCL version 8.6
        # gold on TCL Club, 12Dec2018
        console show
        puts " result value is [expr { 1+3} ] ";
        puts [expr { 1+3} ];
        # creating list
        set lister1 [ list  dog cat lizard ] ;
        puts $lister1;
        set lister2 {" dog cat lizard" };
        puts $lister2;
        #  $lister2 is really a string of 3 items
        # but some list coomands lappend and lindex may
        # interpret $lister2 as list
        set lister3 {" dog cat”  lizard };
        puts $lister3;
        set lister4 " dog cat lizard" ;
        $lister4;
        # note outputs are similar
        # for $lister1 and $lister4
        # gold on TCL Club, 12Dec2018
           

Code Scraps


      # The easy way is to start with proper list command
      set list_fruit [ list apple pear orange grape ]
      lappend $list_fruit peach
      # The hard way is to start with individual strings of characters
      set a1 apple
      set a2 pear 
      set a3 orange
      set a4 grape
      set a5 peach
      set list_fruit_assembly [ list $a1 $a2 $a3 $a4 ]
      lappend list_fruit_assembly $a5      

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 20210913. Thanks for feedback. Maybe my head is turned around. When I write set lister1 { dog cat lizard } # returns dog cat lizard. I 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 commnand 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.


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.