programming.nbk: Home | Index | Next Page: tcl: lappend listVar arg arg... | Previous Page: Tcl: foreach


 Tcl: grouping arguments

Grouping with quotation marks

Words within double quotes are grouped into a single arguement:

    set foo "hello, world" -> hello, world

whereas:

    set foo hello world -> wrong # args: should be "set varName ?newValue?"

Grouping words within double quotes allows substitutions to occur within the quotations.

    puts "The current stock value is $varName"

causes the current value of varName to be substituted for $varName, and then the entire string is printed to the output device.

In general, the backslash (\) disables substitution for the single character immediately following the backslash. Any character immediately following the backslash will stand without substitution.

However, there are specific "Backslash Sequence" strings which are replaced by specific values during the substitution phase. The following backslash strings will be substituted as shown below.

String  Output                  Hex Value 
\a      Audible Bell                0x07 
\b      Backspace                   0x08 
\f      Form Feed (clear screen)    0x0c 
\n      New Line                    0x0a 
\r      Carriage Return             0x0d 
\t      Tab                         0x09 
\v      Vertical Tab                0x0b 
\0dd    Octal Value d is a number from 1-7 
\xhh    Hex Value h is a hex digit 0-9,A-F,a-f 

The final exception is the backslash at the end of a line of text. This causes the interpreter to ignore the newline, and treat the text as a single line of text. The interpreter will insert a blank space at the location of the ending backslash.

Grouping with braces

Grouping words within braces disables substitution within the grouped string. The string is passed to a command exactly as written.

Grouping with square brackets

When words are grouped with square brackets, the first word is interpreted as the name of a command, and the following words are treated as that command's arguments. The results of the command is substituted for the whole group.

    set foo [sin $angle]

causes the value in variable angle to be passed to the command 'sin'. The result of the sin command is then stored in variable foo.


programming.nbk: Home | Index | Next Page: tcl: lappend listVar arg arg... | Previous Page: Tcl: foreach


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time