?? rhl30.htm
字號:
<BR>
<PRE>
<FONT COLOR="#000080">"40,000"
40000
"refer to table 3"</FONT></PRE>
<P>The Tcl set command is used to assign values to variables. The set command can pass either one or two arguments. When two arguments are passed to the set command, the first one is treated as the variable name and the second is the value to assign to
that variable.
<BR>
<P>When the set command is used with only one argument, Tcl expects the argument to be the name of a variable, and the set command returns the value of that variable. The following command assigns the value of 40000 to the variable Monthlysales and then
echoes the value to the screen:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">set Monthlysales 40000</FONT></PRE>
<P>To print the value of the Monthlysales variable to the screen, you would type
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">set num 40</FONT></PRE>
<P>the value 40 would be represented as the character string 40, not as an integer.
<BR>
<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 set 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:
<BR>
<PRE>
<FONT COLOR="#000080">set Monthlysales 40000
expr $Monthlysales * 12</FONT></PRE>
<P>The first command assigns the value of 40000 to the variable Monthlysales. The expr command is used to perform mathematical evaluations of expressions. In this case it takes the value of the variable Monthlysales and multiplies it by 12.
<BR>
<BR>
<A NAME="E69E365"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Tcl Command Substitution</B></FONT></CENTER></H4>
<BR>
<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:
<BR>
<PRE>
<FONT COLOR="#000080">set Monthlysales 40000
set Yearlyforecast [ expr $Monthlysales * 12 ]</FONT></PRE>
<P>The first command once again sets the variable Monthlysales to the value of 40000. The second command makes use of command substitution to set the value of Yearlyforecast equal to the result of the command in the square braces.
<BR>
<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.
<BR>
<BR>
<A NAME="E69E366"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Quotes</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<P>Three kinds of quoting can be used in Tcl scripts. The first is quoting with double quotation marks (""). This kind of quoting is used to hide whitespace characters and command separators from the Tcl interpreter.
<BR>
<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:
<BR>
<PRE>
<FONT COLOR="#000080">set "Monthly sales" 40000
set Heading1 "Description of item"</FONT></PRE>
<P>The second kind of quoting uses the backslash character (\). 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 $, from the Tcl interpreter. The
following example illustrates the need for backslash quoting:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">set Header1 "The cost is \$3.50"</FONT></PRE>
<P>In this example the Header1 variable is being assigned the value The cost is $3.50. 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 $ 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 3.50 into the command. This would result in an
error, because there is no variable with that name defined.
<BR>
<P>The third type of quoting available in Tcl uses curly braces ({}). 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:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">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 while command:
<BR>
<PRE>
<FONT COLOR="#000080">set count 0
while {$count < 3} {
puts "count equals $count"
set count [expr $count + 1]
}</FONT></PRE>
<P>The while 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 while command.
<BR>
<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.
<BR>
<BR>
<A NAME="E69E367"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>if</B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>The if 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 if command is the following:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">if {expr} {commands}</FONT></PRE>
<P>The if 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:
<BR>
<PRE>
<FONT COLOR="#000080">$i < 10
$num = 2</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Tcl treats any expression that evaluates to zero to be False and any expression that evaluates to a non-zero value to be True.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Expressions such as the following, although valid, do not make much sense in the context of an if command:
<BR>
<PRE>
<FONT COLOR="#000080">$i + $b
10 * 3</FONT></PRE>
<P>The second argument to the if 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.
<BR>
<P>The if commands can have one or more elseif commands and one else command associated with them. The syntax for these commands is shown here:
<BR>
<PRE>
<FONT COLOR="#000080">if {expr} {
commands }
elseif {expr} {
commands }
elseif {expr} {
commands }
else {
commands }</FONT></PRE>
<P>The commands associated with the first if or elseif whose expression evaluates to True are executed. If none of these expressions evaluate to True, the commands associated with the else command are executed.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>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 if command where the if {expr} portion of the command appeared on one line and the rest was
on the next line, it would be treated as two separate commands.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E368"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>for</B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>The for command in Tcl provides a way of implementing for loops. Tcl for loops are very similar to for loops in other languages, such as C. The for 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.
<BR>
<PRE>
<FONT COLOR="#000080">for { set i 0} {$i < 10} {incr i 1} {
puts [expr 2 * $i]
}</FONT></PRE>
<P>The preceding loop executes ten times. The counter i is initially set to 0. The for loop executes while i is less than 10, and the value of i is increased by 1 each time through the loop. The command that is executed each time through the loop is the
puts command. This evaluates 2 ´ i each time through the loop and prints the result to the screen. The output that results from running this command is listed here:
<BR>
<PRE>
<FONT COLOR="#000080">0
2
4
6
8
10
12
14
16
18</FONT></PRE>
<BR>
<A NAME="E69E369"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>while</B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>The while command is used to implement while loops in Tcl. while loops are very similar to for loops. The only real difference between them is that the for loop provides more enhanced features for controlling entrance and exit criteria for the loop. The
syntax for the while loop is shown in the following example:
<BR>
<PRE>
<FONT COLOR="#000080">set i 0
while {$i < 10} {
puts [expr 2 * $i]
set i [expr $i + 1]
}</FONT></PRE>
<P>This while loop performs the same function as the example that was presented in the section describing the for loop. It calculates 2 ´ i each time through the loop and prints the result to the screen. Notice that in this example you have to handle
incrementing the counter yourself. With the for loop, the counter incrementing was taken care of by the for command.
<BR>
<BR>
<A NAME="E69E370"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>switch</B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>The switch command provides the same function as an if statement that has multiple elseif clauses associated with it. The switch command compares a value (this value is usually stored in a variable) with any number of patterns, and if it finds a match
it executes the Tcl code associated with the matching pattern.
<BR>
<PRE>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -