分类:
2011-03-17 10:27:35
TCL中,命令的计算分为2个阶段。第一阶段仅仅是替换,第2阶段计算命令的值。只替换一次。
比如在puts $varName ⇒ puts "Hello World"中,就是先把varName置换为”Hello World”,然后再输出。
TCL提供三种形式的置换:变量置换、命令置换和反斜杠置换。上面的例子就是变量置换。
[]中置换为命令的执行结果。
转义字符的置换:
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 digit from 0-7
\uHHHH H is a hex digit 0-9,A-F,a-f. This represents a 16-byte Unicode character.
\xHH.... Hex Value H is a hex digit 0-9,A-F,a-f. Note that the \x substitution "keeps going" as long as it has hex digits, and only uses the last two, meaning that \xaa and \xaaaa are equal, and that \xaaAnd anyway will "eat" the A of "And". Using the \u notation is probably a better idea.
位于行尾的”\” 用于续行。
实例:
set Z Albany
set Z_LABEL "The Capitol of New York is: "
puts "$Z_LABEL $Z" ;# Prints the value of Z
puts "$Z_LABEL \$Z" ;# Prints a literal $Z instead of the value of Z
puts "\nBen Franklin is on the \$100.00 bill"
set a 100.00
puts "Washington is not on the $a bill" ;# This is not what you want
puts "Lincoln is not on the $$a bill" ;# This is OK
puts "Hamilton is not on the \$a bill" ;# This is not what you want
puts "Ben Franklin is on the \$$a bill" ;# But, this is OK
puts "\n................. examples of escape strings"
puts "Tab\tTab\tTab"
puts "This string prints out \non two lines"
puts "This string comes out\
on a single line"
运行结果:
The Capitol of New York is: Albany
The Capitol of New York is: $Z
Ben Franklin is on the $100.00 bill
Washington is not on the 100.00 bill
Lincoln is not on the $100.00 bill
Hamilton is not on the $a bill
Ben Franklin is on the $100.00 bill
................. examples of escape strings
Tab Tab Tab
This string prints out
on two lines
This string comes out on a single line
注意puts "Lincoln is not on the $$a bill" ;# This is OK 体现了只置换一次的原则。