Scratchpad r76 (See the current copy)
- Information about Roy Keene
- Random Scripts
- My Friends
- Random Demonstrations
- Information about Tcl
- Information about Threads
- Random Diatribes
- Information about Random Number Generation
Feel free to contact me about this website at mailto:wiki@rkeene.org
- Information about interesting Current Projects
- My Scratchpad
- Random Links
What you will find here
Reimplementing the If Statement in Tcl
#! /usr/bin/tclsh
# Delete the "if" command
rename if ""
# Make our own.
proc if args {
set result [uplevel 1 [list expr [lindex $args 0]]]
switch -- $result {
"1" {
uplevel 1 [lindex $args 1]
}
default {
switch -- [lindex $args 2] {
"else" {
uplevel 1 [lindex $args 3]
}
}
}
}
}
# Test it
if {1} { puts "Test" }
if {0} { puts "No Test" }
if {1} { puts "Joe" } else { puts "Bob" }
if {0} { puts "Joe" } else { puts "Bob" }
foreach joe [list bob joe sally] {
if {$joe == "joe"} {
puts "Joe = joe !"
} else {
puts "Joe = $joe."
}
}