⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch30.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading8<FONT COLOR="#000077"><B>NOTE: </B></FONT>Quotation marks



	cause Tcl to ignore the whitespace characters (spaces and tabs) in the variable name



	and treat it as one word. This is discussed in the &quot;Quotes&quot; section of



	this chapter.



<HR>







</DL>







<P>The value that is placed into a variable can also be any combination of printable



characters. Possible values for the <TT>Monthly_sales</TT> variable are<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">&quot;40,000&quot;



40000



&quot;refer to table 3&quot;



</FONT></PRE>



<P>The Tcl <TT>set</TT> command is used to assign values to variables. The <TT>set</TT>



command can pass either one or two arguments. When two arguments are passed to the



<TT>set</TT> command, the first one is treated as the variable name and the second



is the value to assign to that variable.</P>



<P>When the <TT>set</TT> command is used with only one argument, Tcl expects the



argument to be the name of a variable, and the <TT>set</TT> command returns the value



of that variable. The following command assigns the value of 40000 to the variable



<TT>Monthlysales</TT> and then echoes the value to the screen:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Monthlysales 40000



</FONT></PRE>



<P>To print the value of the <TT>Monthlysales</TT> variable to the screen, you would



type<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Monthlysales



</FONT></PRE>



<P>All values that are assigned to variables are stored as character strings. If



you defined a variable to be equal to the integer 40, as in the following command<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set num 40



</FONT></PRE>



<P>the value 40 would be represented as the character string <TT>40</TT>, not as



an integer.</P>



<P>So far you have seen how to set variables and how to display their values to the



screen, but you have not seen how they are used with commands other than the <TT>set</TT>



command. To use the value of a variable in another command, you must precede the



variable name with an unquoted dollar sign ($). This tells Tcl to expect a variable



name and to substitute the value of that variable for the variable name. The following



example shows a simple use of variable substitution:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Monthlysales 40000



expr $Monthlysales * 12



</FONT></PRE>



<P>The first command assigns the value of 40000 to the variable <TT>Monthlysales</TT>.



The <TT>expr</TT> command is used to perform mathematical evaluations of expressions.



In this case it takes the value of the variable <TT>Monthlysales</TT> and multiplies



it by 12.



<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Tcl Command Substitution</FONT></H4>



<P>Command substitution provides a way of substituting the result of a Tcl command



(or commands) into an argument of another command. The syntax for command substitution



is to include the commands that are being substituted in square brackets, as follows:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Monthlysales 40000



set Yearlyforecast [ expr Monthlysales * 12 ]



</FONT></PRE>



<P>The first command once again sets the variable <TT>Monthlysales</TT> to the value



of 40000. The second command makes use of command substitution to set the value of



<TT>Yearlyforecast</TT> equal to the result of the command in the square braces.</P>



<P>In this example the substitute consisted of only one command. Tcl allows the substitute



to consist of any valid Tcl script, meaning that it can contain any number of Tcl



commands.



<H4 ALIGN="CENTER"><A NAME="Heading10<FONT COLOR="#000077">Quotes</FONT></H4>



<P>Often you want to use commands containing special characters that you don't want



Tcl to interpret. By quoting these characters, you hide the special characters 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>&quot;&quot;</TT>). This kind of quoting is used to hide whitespace



characters and command separators from the Tcl interpreter.</P>



<P>Whenever you have 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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set &quot;Monthly sales&quot; 40000</FONT></PRE>







<PRE><FONT COLOR="#0066FF">



set Heading1 &quot;Description of item&quot;



</FONT></PRE>



<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>$</TT>, from the Tcl



interpreter. The following example illustrates the need for backslash quoting:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Header1 &quot;The cost is \$3.50&quot;



</FONT></PRE>



<P>In this example the <TT>Header1</TT> variable is being assigned the value <TT>The



cost is $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>$</TT> as a regular character



instead of the variable substitution character. If the backslash was not used in



the command, the interpreter would attempt 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 curly braces (<TT>{}</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 could be written using brace quotation as follows:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set Header1 {The cost is $3.50}



</FONT></PRE>



<P>The most important use of curly 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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set count 0



while {$count &lt; 3} {



      puts &quot;count equals $count&quot;



      set count [expr $count + 1]



}



</FONT></PRE>



<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 you have all the language basics out of the way, you can move on to some



of the more advanced Tcl commands.



<H4 ALIGN="CENTER"><A NAME="Heading11<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. This is



the syntax of the <TT>if</TT> command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">if {expr} {commands}



</FONT></PRE>



<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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$i &lt; 10



$num = 2



</FONT></PRE>







<DL>



	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading12<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Tcl treats any



	expression that evaluates to zero to be False and any expression that evaluates to



	a non-zero value to be True.



<HR>







</DL>







<P>Expressions such as the following, although valid, do not make much sense in the



context of an <TT>if</TT> command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$i + $b



10 * 3



</FONT></PRE>



<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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">if {expr} {



      commands }



elseif {expr} {



      commands }



elseif {expr} {



      commands }



else {



      commands }



</FONT></PRE>



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







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading13<FONT COLOR="#000077"><B>WARNING:</B> </FONT>The open curly



	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 entered an <TT>if</TT> command where



	the <TT>if {</TT>expr<TT>}</TT> portion of the command appeared on one line and the



	rest was on the next line, it would be treated as two separate commands.



<HR>







</DL>







<H4 ALIGN="CENTER"><A NAME="Heading14<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.<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">for { set i 0} {$i &lt; 10} {incr i 1} {



      puts [expr 2 * $i]



}



</FONT></PRE>



<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 2 x <TT>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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">0 



2 



4 



6 



8 



10 



12 



14 



16 



18



</FONT></PRE>



<H4 ALIGN="CENTER"><A NAME="Heading15<FONT COLOR="#000077">The while Command</FONT></H4>



<P>The <TT>while</TT> command is used to implement <TT>while</TT> loops in Tcl. <TT>while</TT>



loops are very similar to <TT>for</TT> loops. The only real difference between them



is that the <TT>for</TT> loop provides more enhanced features for controlling entrance



and exit criteria for the loop. The syntax for the <TT>while</TT> loop is shown in



the following example:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">set i 0



while {$i &lt; 10} {

⌨️ 快捷键说明

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