What's My Line? is a game-style exercise where the goal is to guess the output of the Tcl script
The following Tcl scripts are arranged in ascending order roughly according to the level of challenge the pose. The task is simple: read a script, and then guess what the output will be. Actually writing down your answer before looking at the answer is highly recommended. When you get something wrong, spend some time writing similar scripts in order to explore the subject in more detail.
Size your browser window so that the answer is not visible while you form your own answer.
Whitespace, Interrupted
list {*}\ {one two}
Answer:
umatched open brace in list
There are three words in the command: list, {*} {one, and two} The word which follow {*} must be a properly-formed list, but {one (with a space preceeding the brace) isn't. (Thanks CMcC for the example, Tcl Chatroom, 2014-11-28).
proc proc {args} { puts {No soup for you!} } proc hello {} { puts {Hi there!} } catch hello res puts $res
Answer:
No soup for you! invalid command name "hello"
The built-in [proc] is placed by a user-defined procedure that, instead of creating new procedures, prints No soup for you!. proc hello... then doesn't create a hello procedure, so when an attempt is made to invoke hello, no such command is found, and an error is produced instead.
Prior to running this script create a file named data that contains:
#this file should be called "data" set localvar 99
namespace eval ns1 { proc proc1 {} { upvar 0 [namespace current]::var1 localvar source data set var2 Hello } proc1 } catch {set ns1::var1} eres einfo puts $eres catch {set ns1::var2} eres einfo puts $eres catch {set $var2} eres einfo puts $eres
answer:
99 can't read "ns1::var2": no such variable can't read "var2": no such variable
[source] causes a script to be evaluated in the current scope.
set generate {apply {length { while {[incr length -1] > -1} { set rand [expr {int(rand()*26)}] set offset [lindex {65 97} [expr {int(rand()*10) % 2}]] append res [format %c [expr {$rand + $offset}]] } return $res }}} puts [{*}$generate 50]
Answer:
A string of 50 random letters, randomly capitalized.
generate could be the arguments and body of a procedure. [apply] executes $generate directly, no need to actually create a procedure. In ASCII, upper-case English letters start at position 65, and lower-case letters start at position 97. To choose a random letter, a random number between 0 and 26 is first obtained, and then either 65 (for upper case) or 97 (for lower case) is added to that number. [format] is then used to obtain the ASCII letter that corresponds with that number. This process is repeated until the string of desired length is built up.