Tcl Example


Tcl is an interpreted programming language like Perl and Python (although there’s some big differences).  To give you an idea of what a Tcl program looks like, I’ve written this little program which can be run using your friendly Tcl interpreter.

#
#  Fun with TCL at Cisco Live 2009
#  Jacob Uecker
#  juecker.net
#

# Outputs the string to the screen
puts "Enter a number:"

# Gets input from the user and stores into variable 'num'
gets stdin num

# Loop
#  Sets the for loop variable 'i' to the value that the user entered
#  Runs as long as the variable is greater than zero
#  Decrements the value by one each time
for { set i $num } { $i >= 0 } { set i [expr {$i - 1}] } {

	# Outputs the current value of i
	puts "The variable \$i is currently: $i" 

	# If the value of i is zero output a special message
	if { $i == 0 } {
		puts "Learning Tcl is fun when \$i is zero."
	#end if
	}

# End for loop
}
  1. No comments yet.
(will not be published)

  1. No trackbacks yet.