529-532.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 160 行

HTML
160
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Introduction to Tcl and Tk</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=29//-->

<!--PAGES=529-532//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="526-529.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="532-535.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Quotes</FONT></H4>

<P>There may be times when you&#146;ll want to use commands containing special characters that you don&#146;t want Tcl to interpret. By quoting these particular characters, you hide them from the Tcl interpreter.

</P>

<P>Three kinds of quoting can be used in Tcl scripts. The first is quoting with double quotation marks (<TT>&#147;&#148;</TT>). This kind of quoting is used to hide whitespace characters and command separators from the Tcl interpreter.</P>

<P>Whenever there are two or more words that you want Tcl to treat as a single word, you can do so by surrounding the words with quotation marks. A good example of this type of quoting is when you want to create variable names that contain more than one word or you want to assign a value that contains more than one word to a variable, as follows:</P>

<!-- CODE SNIP //-->

<PRE>

set &#147;Monthly sales&#148; 40000



set Heading1 &#147;Description of item&#148;

</PRE>

<!-- END CODE SNIP //-->

<P>The second kind of quoting uses the backslash character (<TT>\</TT>). This type of quoting can hide any single character from the interpreter. This form of quoting is most commonly used to hide special characters, such as <TT>&#36;</TT>, from the Tcl interpreter. The following example illustrates the need for backslash quoting:</P>

<!-- CODE SNIP //-->

<PRE>

set Header1 &#147;The cost is \&#36;3.50&#148;

</PRE>

<!-- END CODE SNIP //-->

<P>In this example the <TT>Header1</TT> variable is being assigned the value <TT>The cost is &#36;3.50</TT>. The quotation marks are necessary to hide the fact that there are four separate words contained in the character string. The backslash in this command tells the Tcl interpreter to treat the <TT>&#36;</TT> as a regular character instead of the variable substitution character. If the backslash had not been used in the command, the interpreter would have attempted to substitute the variable named <TT>3.50</TT> into the command. This would result in an error because there is no variable with that name defined.</P>

<P>The third type of quoting available in Tcl uses braces (<TT>&#123;&#125;</TT>). This quoting is more powerful than quoting using quotation marks or backslashes. Quoting using braces hides not only whitespace and command separators from the Tcl interpreter, but also any other kind of special character. This type of quoting can be used to eliminate the need for backslash quoting in character strings. The example used for backslash quoting can be written using brace quotation as follows:</P>

<!-- CODE SNIP //-->

<PRE>

set Header1 &#123;The cost is &#36;3.50&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The most important use of brace quoting is to defer the evaluation of special characters. This means that special characters are not processed immediately by the Tcl interpreter but are instead passed to a command that processes the special characters on its own. An example of when deferred evaluation is used is in the <TT>while</TT> command:</P>

<!-- CODE SNIP //-->

<PRE>

set count 0

while &#123;&#36;count &lt; 3&#125; &#123;

   puts &#147;count equals &#36;count&#148;

   set count [expr &#36;count &#43; 1]

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>while</TT> command has to evaluate both of its arguments each time it iterates through the loop. It is therefore necessary for the interpreter to ignore the special characters in both of these arguments and leave the evaluation of these characters up to the <TT>while</TT> command.</P>

<P>Now that we have all the language basics out of the way, let&#146;s move on to some of the more advanced Tcl commands.</P>

<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">The if Command</FONT></H4>

<P>The <TT>if</TT> command, just like in other languages, evaluates an expression and, based on the results of the expression, executes a set of commands. The syntax of the <TT>if</TT> command is the following:</P>

<!-- CODE SNIP //-->

<PRE>

if &#123;expr&#125; &#123;commands&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>if</TT> command expects two arguments. The first argument is an expression that is used to determine whether to execute the commands contained in the second argument. The expression argument is typically an expression that evaluates to either True or False. For example:</P>

<!-- CODE SNIP //-->

<PRE>

&#36;i &lt; 10

&#36;num = 2

</PRE>

<!-- END CODE SNIP //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>Tcl treats any expression that evaluates to zero to be False and any expression that evaluates to a nonzero value to be True. As with other programming languages like C and Perl, this is the opposite of Linux utilities like <TT>test</TT>. You simply have to familiarize yourself with each and get to know which are which.

<HR></FONT>

</BLOCKQUOTE>

<P>Expressions such as the following, although valid, do not make much sense in the context of an <TT>if</TT> command:</P>

<!-- CODE SNIP //-->

<PRE>

&#36;i &#43; &#36;b

10 * 3

</PRE>

<!-- END CODE SNIP //-->

<P>The second argument to the <TT>if</TT> command is a Tcl script, which can contain any number of Tcl commands. This script is executed if the expression contained in the first argument evaluates to True.</P>

<P>The <TT>if</TT> commands can have one or more <TT>elseif</TT> commands and one <TT>else</TT> command associated with them. The syntax for these commands is shown here:</P>

<!-- CODE SNIP //-->

<PRE>

if &#123;expr&#125; &#123;

   commands &#125;

elseif &#123;expr&#125; &#123;

   commands &#125;

elseif &#123;expr&#125; &#123;

   commands &#125;

else &#123;

   commands &#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The commands associated with the first <TT>if</TT> or <TT>elseif</TT> whose expression evaluates to True are executed. If none of these expressions evaluate to True, the commands associated with the <TT>else</TT> command are executed.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Warning:&nbsp;&nbsp;</B><BR>The open brace must occur on the same line as the word that precedes it. This is because new lines are treated as command separators. If you enter an <TT>if</TT> command where the <TT>if &#123;expr&#125;</TT> portion of the command appears on one line and the rest is on the next line, it is treated as two separate commands.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">The for Command</FONT></H4>

<P>The <TT>for</TT> command in Tcl provides a way of implementing <TT>for</TT> loops. Tcl <TT>for</TT> loops are very similar to <TT>for</TT> loops in other languages, such as C. The <TT>for</TT> command expects four arguments. The first argument is a script that is used to initialize the counter. The second argument is an expression that is evaluated each time through the loop to determine whether to continue. The third argument is used to define the increment to be used on the counter. The fourth argument is the set of commands to be executed each time through the loop.</P>

<!-- CODE SNIP //-->

<PRE>

for &#123; set i 0&#125; &#123;&#36;i &lt; 10&#125; &#123;incr I 1&#125; &#123;

   puts [expr 2 * &#36;i]

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The preceding loop executes ten times. The counter <TT>i</TT> is initially set to 0. The <TT>for</TT> loop executes while <TT>i</TT> is less than 10, and the value of <TT>i</TT> is increased by 1 each time through the loop. The command that is executed each time through the loop is the <TT>puts</TT> command. This evaluates <TT>2 * i</TT> each time through the loop and prints the result to the screen. The output that results from running this command is listed here:</P>

<!-- CODE //-->

<PRE>

0

2

4

6

8

10

12

14

16

18

</PRE>

<!-- END CODE //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="526-529.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="532-535.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?