Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switching on variables 1

Status
Not open for further replies.

tomdagliusa

Programmer
May 31, 2000
34
US
If I predefine a var, and try to switch on it's value, it doesn't work.

tcl>set x "10.1.1.1"
tcl>puts $x
10.1.1.1
tcl>set y $x
tcl>puts $y
10.1.1.1
I would like to know if y is equal to x:
tcl>switch -exact -- $y {
=>$x {puts "x"}
=>2 {puts "2"}
=>default {puts $y}
=>}
10.1.1.1
Even though y and x are both "10.1.1.1", they don't
equate within the switch, but they will outside the
switch:

tcl>if {$x == $y} {
=>puts "yes"
=>} else {
=>puts "no"
=>}
yes

???,
Tom
 
For an extended discussion of this, go to the Tcl'ers Wiki ( and check out the page "switch," The basic problem is that the curly braces around the pattern-action block prevents the Tcl interpreter from performing substitutions on the patterns. So, switch is testing against the literal pattern "$x" in your example. To allow the Tcl interpreter to perform substutions on the patterns, you need to use the alternate syntax of switch in which each pattern and action are presented as seperate command-line arguments, rather than as part of a monolithic pattern-action block:

[tt]% set x "10.1.1.1"
10.1.1.1
% puts $x
10.1.1.1
% set y $x
10.1.1.1
% puts $y
10.1.1.1
% switch -exact -- $y \
> $x {puts "x"} \
> 2 {puts "2"} \
> default {puts $y}
x[/tt]

At this point, many people start worrying about the curly-brace-quoting of the action blocks. Don't they prevent substitutions from occurring in all the actions? Yes, they do. More specifically, they prevent premature substitutions from occurring. After all, we don't want the Tcl interpreter performing variable and command substitutions on all of the actions given to switch (or if or while or any other Tcl control structure) before switch has determined which action needs to be executed. So we curly-brace quote the actions so that they are passed unsubstituted as arguments to switch. The switch command then performs its test and determines which action (if any) to execute. Once that determination is made, the switch command passes the action to the Tcl interpreter, which follows standard Tcl parsing rules to evaluate and execute the commands contained in that action.

Okay, so if the curly braces prevent substitutions, why does your if expression work? This is a special case. All Tcl control structures that accept a Boolean condition perform an additional round of substituations on the condition when they evaluate it (by internally passing the condition to the expr command, it turns out). Why do they do this? Consider the following example:

Code:
set i 0
while $i<10 {
  puts $i
  incr i
}

This while loop runs forever. Why? Because the Tcl interpreter performs substitution on the arguments before passing them to the command. The Tcl interpreter is a very simplistic beast -- it has no idea what the command is going to do, it just acts as a parser. In this example, the Tcl interpreter has no idea that the command is going to repeatedly execute one of its arguments as a chunk of Tcl code. The big chunk of Tcl code is a curly-brace quoted argument, so the Tcl interpreter passes it to while without substitution. But the expression is not protected, so the Tcl interpreter substitutes it as &quot;0<10&quot; before passing it to while. Because the expression has now been hard-coded, it always evaluates to True, and the loop continues forever. That's why we need to protect the expression from substutition by the Tcl interpreter while it is parsing the arguments, and instead allows the while command to implicitly perform the substitution each time through the loop:

Code:
set i 0
while {$i<10} {
  puts $i
  incr i
}

That's probably more than you wanted to know about this subject, but I thought I'd try to be complete. It's the most subtle aspect of Tcl's behavior. And while it might seem arcane at first, it turns out to be very useful behavior. All control structures are implemented as Tcl commands. Which allows programmers to add their own control structures as desired (with a little help from uplevel, which I won't get into here :) ), a capability very few other languages provide.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks Ken. I get the gist of your explanation, but I printed it out for study tonight, and I'll take a look at the extended discussions you suggested.

Regards,
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top