📄 x-tclsumm2.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title> Tcl </title></head><body bgcolor="FFFFFF"><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="x-tclsumm.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="x-tclsumm1.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="x-tclsumm3.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="83939">B.2 A Taste of Tcl</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83940"> </a>Tcl represents all data as ordinary text strings. As you might expect, the string-handling features of Tcl are particularly strong. However, Tcl also provides a full complement of C-like arithmetic operators to manipulate strings that represent numbers.</p><dd><p class="Body"><a name="83941"> </a>The examples in the following sections exhibit some of the fundamental mechanisms of the Tcl language, in order to provide some of the flavor of working in Tcl. However, this is only an introduction. </p><dd><p class="Body"><a name="83942"> </a>For documentation on all Tcl interfaces in Tornado (as well as on C interfaces), see the <i class="title">Tornado API Guide</i> from Wind River Systems. </p><dd><p class="Body"><a name="83943"> </a>For the Tcl language itself, the following generally available books are helpful:</p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84383"> </a>Ousterhout<i class="title">, </i>John K.: <i class="title">Tcl and the Tk Toolkit </i>(Addison-Wesley, 1994) - The definitive book on Tcl, written by its creator. </li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84385"> </a>Welch, Brent: <i class="title">Practical Programming in Tcl and Tk</i> (Prentice Hall, 1995) - Useful both as a quick Tcl reference and as a tutorial.</li></ul></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="83948">B.2.1 Tcl Variables</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83950"> </a>The Tcl <b class="tclProc">set</b> command defines variables. Its result is the current value of the variable, as shown in the following examples:<p class="table"><h4 class="EntityTitle"><a name="83953"><font face="Helvetica, sans-serif" size="-1" class="sans">Table B-1: Setting Tcl Variables</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="83957"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Tcl Expression</font></b></div></th><td width="10"> </td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="83959"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Result</font></b></div></th><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83965">set num 6</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83967"></b><tt class="output">6</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83969">set y hello</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83971"></b><tt class="output">hello</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83973">set z "hello world"</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83975"></b><tt class="output">hello world</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83977">set t $z</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83979"></b><tt class="output">hello world</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83981">set u "$z $y"</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83983"></b><tt class="output">hello world hello</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83985">set v {$z $y} </a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="83987"></b><tt class="output">$z $y</tt><b></a></b></pre></td><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p><dd><p class="Body"><a name="83992"> </a>The expressions above also illustrate the use of some special characters in Tcl:</p></dl><dl class="margin"><dd><div class="Item"><a name="83993"> </a><kbd>SPACE</kbd> </div><dl class="margin"><dd><div class="Indent"><a name="84404"> </a>Spaces normally separate single words, or tokens, each of which is a syntactic unit in Tcl expressions.</div><br></dl><dd><div class="Item"><a name="83994"> </a><b class="symbol_lc">" "</b> </div><dl class="margin"><dd><div class="Indent"><a name="84406"> </a>A pair of double quotes groups the enclosed string, including spaces, into a single token.</div><br></dl><dd><div class="Item"><a name="83995"> </a><b class="symbol_lc">$</b><i class="textVariable">vname</i> </div><dl class="margin"><dd><div class="Indent"><a name="84408"> </a>The <b class="operator">$</b> character normally introduces a variable reference. A token <b class="operator">$</b><i class="textVariable">vname</i> (either not surrounded by quotes, or inside double quotes) substitutes the value of the variable named <i class="textVariable">vname</i>.</div><br></dl><dd><div class="Item"><a name="83996"> </a><b class="symbol_lc">{ }</b> </div><dl class="margin"><dd><div class="Indent"><a name="84410"> </a>Curly braces are a stronger form of quoting. They group the enclosed string into a single token, and also prevent any substitutions in that string. For example, you can get the character <b class="operator">$</b> into a string by enclosing it in curly braces.</div><br></dl></dl><dl class="margin"><dd><p class="Body"><a name="84023"> </a>With a single argument, <b class="tclProc">set</b> gives the current value of a variable:<p class="table"><h4 class="EntityTitle"><a name="83999"><font face="Helvetica, sans-serif" size="-1" class="sans">Table B-2: Evaluating Tcl Variables</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84003"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Tcl Expression</font></b></div></th><td width="10"> </td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84005"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Result</font></b></div></th><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84011">set num</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84013"></b><tt class="output">6</tt><b></a></b></pre></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84015">set z</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84018"></b><tt class="output">hello world</tt><b></a></b></pre></td><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84025">B.2.2 Lists in Tcl</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84028"> </a>Tcl provides special facilities for manipulating lists. In Tcl, a <i class="term">list</i> is just a string, with the list elements delimited by spaces, as shown in the following examples:<p class="table"><h4 class="EntityTitle"><a name="84032"><font face="Helvetica, sans-serif" size="-1" class="sans">Table B-3: Using Tcl Lists</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84038"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Tcl Expression</font></b></div></th><td width="10"> </td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84040"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Result</font></b></div></th><td width="10"> </td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84042"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th><td width="10"> </td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84050">llength $v</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84052"></b><tt class="output">2</tt><b></a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="84054"> </a>Length of list <b class="symbol_lc">v</b>.</div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84056">lindex $u 1</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84058"></b><tt class="output">world</tt><b></a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="84060"> </a>Second element of list <b class="symbol_lc">u</b>.</div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84438">set long "a b c d e f g"</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84064"></b><tt class="output">a b c d e f g</tt><b></a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="84066"> </a>Define a longer list.</div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84068">lrange $long 2 4</a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84070"></b><tt class="output">c d e</tt><b></a></b></pre></td><td width="10"> </td><td colspan=1 rowspan=1><div class="CellBody"><a name="84072"> </a>Select elements 2 through 4 of list <b class="symbol_lc">long</b>.</div></td><td width="10"> </td></tr><tr valign="top"><td colspan=1 rowspan=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -