Navigation Logo 2.11  Solutions to Exercises Navigation Logo

 

 

Solution To Exercise 2.2a

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

Solution To Exercise 2.5a

set X {Hello World}

Solution To Exercise 2.5b

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.

Solution To Exercise 2.6a

$X World
Hello World
{Hello} {World} 
{Hello} {World} 

Solution To Exercise 2.7a

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

Solution To Exercise 2.7b

  1. Command substitution for the overall command line is begun first. As a part of command substitution, the command
    set Y $Z
    
    is executed. In an early phase of executing this command, variable substitution is begun and completed. Because of the variable substitution, this command is actually executed:
    set Y 2
    
    Then, the command substitution of the overall command line is completed. The value substituted is the value returned by the set command, that is, 2. Because of command substitution, the overall command line looks like
    set X 2
    
    before it is actually executed. The return value of this set command is thrown away.

  2. set Z [set X]_ext

Solution To Exercise 2.8a

set InFile [open $InputFileName r]
set OutFile [open $OutputFileName w]

Solution To Exercise 2.9a

#!/usr/local/bin/tclsh
while {-1 != [gets stdin Line]} {
  puts $Line
}

Solution To Exercise 2.9b

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.

Solution To Exercise 2.9c

You either accomplish this activity or you do not. No answer is possible here.

Solution To Exercise 2.10a

% 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
$X
This 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"

Solution To Exercise 2.10b

If you enter

source C:\DOSNAME
the 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}
 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.