Hai’s Blog

April 23, 2009

Boolean logic in Tcl

Filed under: Programming, Tcl — Hai @ 8:04 pm

Many people who write Tcl script get confused when it comes to boolean logic, especially within if, while construct. To clear up some of these confusions, let’s take a look at the following interactive session:

	(tcl) 66 % # What is true?
	(tcl) 66 % set var -1
	-1
	(tcl) 67 % string is true $var
	0
	(tcl) 68 % # So, -1 is not considered a token for true. Let see now:
	(tcl) 68 % if {$var} {puts "$var is true"}
	-1 is true
	(tcl) 69 % # It seems the expression in the if statement only care if var is zero or not
	(tcl) 69 % # Try a couple of other tokens:
	(tcl) 69 % string is true yes
	1
	(tcl) 70 % string is true no
	0
	(tcl) 71 % string is false no
	1
	(tcl) 72 % # So yes=true, no=false, so far so good
	(tcl) 72 % set var yes
	yes
	(tcl) 73 % if {$var} {puts "$var is true"}
	yes is true
	(tcl) 74 % set var neither
	neither
	(tcl) 75 % if {$var} {puts "$var is true"}
	expected boolean value but got "neither"
	(tcl) 76 % # Now that is the behavior I expected
	(tcl) 76 % set var no
	no
	(tcl) 77 % if {!$var} {puts "$var is false"}
	no is false
	(tcl) 78 % # Worked as expected
	(tcl) 78 %

Normally, Tcl defines true as 1 and false as 0, but as line 68 shows, Tcl considers any non-zero numerical value as true, not just 1.

Lines 69-77: Tcl’s boolean vocabulary also extends to other tokens such as true/false, y/n, yes/no, and on/off. These tokens are case insensitive, which means Yes, yes, and YES are the same.

Line 74-75: If Tcl does not understand a token, it will flag as an error.

Here are the summary points:

  1. For numeric values 0=false and non-zero=true
  2. For non numeric values, Tcl understands true/false, yes/no, y/n, and on/off. The language might understand more tokens than that, but those are the ones I tested.
  3. Anything other than 1 and 2 will raise an error

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.