529 532




Linux Unleashed, Third Edition:Introduction to Tcl and Tk





-->















Previous
Table of Contents
Next




Quotes
There may be times when you’ll want to use commands containing special characters that you don’t want Tcl to interpret. By quoting these particular characters, you hide them from the Tcl interpreter.

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


set “Monthly sales” 40000

set Heading1 “Description of item”


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:


set Header1 “The cost is \$3.50”


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 had not been used in the command, the interpreter would have attempted 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.
The third type of quoting available in Tcl uses 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 can be written using brace quotation as follows:


set Header1 {The cost is $3.50}


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 while command:


set count 0
while {$count < 3} {
puts “count equals $count”
set count [expr $count + 1]
}


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.
Now that we have all the language basics out of the way, let’s move on to some of the more advanced Tcl commands.
The if Command
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:


if {expr} {commands}


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:


$i < 10
$num = 2



Note:  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 test. You simply have to familiarize yourself with each and get to know which are which.


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


$i + $b
10 * 3


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


if {expr} {
commands }
elseif {expr} {
commands }
elseif {expr} {
commands }
else {
commands }


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.

Warning:  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 if command where the if {expr} portion of the command appears on one line and the rest is on the next line, it is treated as two separate commands.

The for Command
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.


for { set i 0} {$i < 10} {incr I 1} {
puts [expr 2 * $i]
}


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:


0
2
4
6
8
10
12
14
16
18






Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
526 529
kosiarka solo 530 531 532 532p
527 529
532 (2)
readme (532)
532 (3)
530 532
README (529)
529 530
532 535
Instrukcja GECO Z 532 P01 S v01 w02 POL
529 (2)

więcej podobnych podstron