Version 26 of TCL programs for beginners 1-10

Updated 2013-03-01 21:48:15 by pooryorick

Summary

The following basic programs written in TCL will give you a general introduction to the TCL syntax.

Tcl Syntax

aa - Tcl syntax isn't anything like C syntax. It's unlike the syntax of most other popular languages. It's supremely simple and straightforward. These basic programs serve mostly to show the absence of the kind of incantations that are required when programming in a language like C.

aa I think the Endekalogue provides a more useful introduction to Tcl syntax, though it's likely that only someone who is already familiar with Tcl's quoting and substitution rules -- and who isn't burdened by trying to see it in a C-like context -- will understand it easily.

unperson The Endekalogue is fine but most people understand better through examples like we provide here. In any case the Endekalogue and this page provide two different approaches: the best of both worlds. Both can co-exist. Thanks for your understanding.

Hello world

In this example, a file called hello.tcl contains the following text:

#! /bin/env tclsh
puts "Hello world!"
exit 0

To run this script:

tclsh hello.tcl

In the above example, it may be necessary to replace "tclsh" with "tclsh.exe", "tclkit", "tclkit.exe" or whatever your Tcl interpreter is called.

LES: calling 'exit' at the end of every script can be a good habit. Especially if you use Windows and/or ActiveTcl and have .tcl files associated with wish. Scripts run with wish won't exit (and/or return output) until you close them manually if there is no exit command.

Note that if you are on Unix, there's no requirement to end the file name in .tcl . However, by doing so, then on Windows, one can associate the tclkit with the file. And Unix users who use a GUI file manager can do the same thing. And MacOS X users can do the same thing. Thus, instructions can be written in a more cross-platform manner.

LV On Windows, the first line is probably not relevant until you start using Cygwin or some other Unix emulation on MS-DOS package. It is my understanding that the default command processor on Windows ignores the #! line anyways. So on Windows, you would either type:

tclkit.exe hellowworld.tcl

or you would go through whatever the steps are to associate tclkit as the program to use with files that end in .tcl .

A Program that Prompts the User for Input

This program gets some input from the user, stores it in a variable, and prints it on the screen.

puts "Hey dude, how old might you be?  Type your answer and hit 'Enter':  "
gets stdin age
puts "Dude, you are $age years old but you look older."
exit 0

Practice exercise:

Ask a person in which year he/she will retire.

Syntax of variables

Variable names can be made up of any characters. The following is an example of various valid variable names:

set age 42
puts $age

set Age 42
puts $Age

set AGE 42
puts $AGE

set "my age" 42
puts ${my age}

puts "My AGE" 42
puts ${My AGE}

Notice that variable names with whitespace characters in them need to have curly brackets around them for Tcl to uderstand them.

See Also

what kinds of variable names can be used in Tcl?
what kinds of variables can Tcl scripts use?

What is stdin

FW: It stands for Standard Input. All programming languages support this, including C. It means the channel to receive input from (there's also an an stdout, and an stderr). This is usually commands typed in the console, but a program can be for example invoked by another program, which sends it data.

Conditionals

Ask a person his/her age. If it is under 20, reply: "you are a child or a teenager". If it is 20 or over, we reply: "you are an adult now".'''

puts "Hey dude, how old might you be?"
gets stdin Age
if {$Age < 20} { 
    puts "You are a child or a teen-ager"
} else { 
    puts "You are an adult now" 
}

If the user entered something that wasn't a number, there would be an error, but that's a topic for another time.

RS: Alternatively, one could use the arithmetic if operator, replacing the last 5 lines with:

puts "You are [expr {$Age<20? {a child or a teen-ager}: {an adult now}}]"

In other languages, if and else might be considered "statements", but in Tcl, they are just commnands. Tcl doesn't have a concept called "statement". Tcl features like the uplevel command makes it possible to design constructs that in other language, require additional conecpts like "statement".

A more concise example of the same program:

puts "Hey dude, how old might you be?"
gets stdin Age
puts "You are [expr {$Age<20? {a child or a teen-ager}: {an adult now}}]"

Adding Complexity

In the following example, there are three possible conditions, rather than two:

puts "Hey dude, how old might you be?"
gets stdin Age
if {$Age < 12} {
    puts "You are a child"
} elseif {$Age < 19} {
    puts "You are a teen"
} else { 
    puts "You are an adult now" 
}

The following isn't necessarily an example of good style, but it does work:

puts "Hey dude, how old might you be?"
gets stdin Age
puts "You are a[expr {$Age < 12 ? " child" : ($Age < 19 ? " teen" : "n adult now")}]

Checking for an Age Range

puts "Hey dude, how old might you be?"
gets stdin Age
if {$Age >= 0 && $Age <= 12} {
    puts "You are a child."
} elseif {$Age >= 13 && $Age <= 19} {
    puts "You are a teen."
} elseif {$Age > 19}  { 
    puts "You are an adult now."
}

Another way to write the same program:

puts "Hey dude, how old might you be?"
gets stdin Age
if {$Age <= 12} {
    puts "You are a child."
} elseif { $Age <=19 } {
    puts "You are a teen."
} else {
    puts "You are an adult now."
}

This seems to be a use case for the comb pattern:

puts "You are a(n) [comb $Age {child 13 teen 19 adult}]"

Just to show (off) that functional programming can be put to surprising uses all over the place. In Tcl, you can code like in C, but maybe you shouldn't...