| 2.11 Solutions to Exercises |
|
|
The first set statement assigns the string Y to be the value of the variable named with the string X. In other words, $X represents the value Y. Since variable substitution takes place before the arguments are passed to set, the second set statement is equivalent to set Y 2Hence Y becomes a variable containing the character 2. This is not the number 2 as used in computer calculations because the values of variables can only be strings or arrays of strings. The $X in the third set statement works the same way as the second. (This is because the $ only applies to as many following characters as there are letters, numbers, or underscores.) The value of Z is Y.TCL.
set X {Hello World}
X is a string containing the numeric character 2. Y is a string containing the character "$" followed by the character "X." Why? Because the curly brackets suppress dollar sign substitution. The set procedure does not execute any statements, it just assigns the value it gets to a variable. Therefore, unlike the while procedure, the set procedure has no reason to perform variable substitution on any of its arguments. Z is "VAR-{2}" Why? Curly brackets that do not surround an argument have no effect on substitutions.
$X World
Hello World
{Hello} {World}
{Hello} {World}
It was mentioned above in Reading this Book that when the return string of Tcl commands is empty, it will not be mentioned. Such was the case with puts. Because of the square brackets, command substitution happens before the command line shown in the exercise is executed. The result of that is that "Hellow Orld" is printed and the empty string is returned. This empty string becomes the whole command line. Hence the error message "empty command name ""."
set InFile [open $InputFileName r] set OutFile [open $OutputFileName w]
#!/usr/local/bin/tclsh
while {-1 != [gets stdin Line]} {
puts $Line
}
The first time the while loop is executed, gets obtains a line of input and returns it. If that line consists of 0, the test in the while loop causes the loop to terminate. If the line consists of another number, the loop begins executing. However, reading input and testing whether it is 0 all happen before the while loop is executed. Execution of the while loop is set up as
while "1==1" "puts {another line} "
or
while "0==1" "puts {another line} "
This is why it will either stop immediately or run forever without bothering to
get new input. The reason why the expression 0==[gets stdin] is not
reevaluated is that it is in quotes and command substitution is not
suppressed.
To get the expression 0==[gets stdin] passed to the while procedure without substitution, use curly brackets. The while procedure will then perform substitution at the beginning of each iteration. You either accomplish this activity or you do not. No answer is possible here.
% puts "\"" "This prints a double quote because of backslash substitution.
% puts {\"}
\"
This prints two symbols, a backslash and a double quote, because backslash substitution is suppressed. % puts \$X $XThis would do the same thing if the argument were surrounded by quotes. Backslash substitution produces $X. There is no second round of substitution.
% puts "[set X]{$X}!"
Zip{Zip}!
Both command and variable substitution take place. Note that the {} do not
surround the argument, double quotes do.
% puts {[set X]"$X"}
[set X]"$X"
% puts "\ HI" HI
% puts {\
HI}
HI
% puts "\44" $because 44 is the octal code for "$."
% puts {\44}
\44
because no substitution takes place.
% puts one{\44}more\ \}
one{$}more }
Backslash substitution has happened three times.
% puts one{\44}more }
An error occurs because the second } is seen as a second argument. Having
two arguments makes the string, one{$}more, a file reference.
% set X 1; while {$X} "puts \44X; set X 0"
1
This is tricky. Because of the double quotes, "\44" is converted to "$"
before BODY is passed as an argument to while. During each
iteration, while does its own substitutions. There is one iteration during
which $X is substituted with 1.
Do not write control statements such as while and if without using the curly brackets! If you need another lesson, ask yourself what this would do set X 1; while $X "puts \44X; set X 0" If you enter source C:\DOSNAMEthe D will be quoted DOS will get this file name C:OSNAME. Instead, you can do one of these:
source C:\\DOSNAME
source {C:\DOSNAME}
|
Author's Home Page |
|
Order from Amazon. |