[Robert Abitbol] The following basic programs written in TCL will give you a general introduction to the TCL syntax. Code written by [FW], [LV] and [RS]. Many thanks to them! ----- '''Re: 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. ----- '''The''' [Endekalogue] '''and this page''' [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. [RobertAbitbol] 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. ----- '''Program # 1: Hello world. How to start and end a program; how to write something on the screen''' puts "Hello world!" exit 0 ---> Save the program with the extension: [BR] Two of the implied questions seem non-Tcl to me. You don't "end a program." You write libraries and in the end you call one of the library functions to start the program. In this example the library function is already written, it's called "puts". This is a good mental model in other languages too, BTW. Anyway, "exit 0" is superfluous, just like exit(0) in a C main() function would be as well. Also you don't need to save the file with some pre-defined extension. The Tcl startup files are written in Tcl and they are called .tclshrc and .wishrc. Files with the extension .tml often contain Tcl code to produce HTML files etc. Tcl itself doesn't care what form the names of the files have. [Robert Abitbol] OK so I have the TCL kit and I want to execute a small function called Hello world. Can you tell how to go about it step by step? ----- '''Program # 2: an input is asked from the user; putting a variable in memory and printing it on the screen''' puts "Hey dude, how old might you be?" 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''' So far as I know, variable names can contain pretty much '''anything''', including spaces, and there's no requirement for the first letter to be capitalized. In fact, that would go against many programming "styles", where '''Capitalized''' and '''UPPERCASE''' and '''lowercase''' have specific implications. And it goes against the example given, which uses a variable named '''age''' with no upper case...or did until someone changed it to '''Age''' for no reason I can see. [BR] Variables can have any string as their name. But to use the usual $name syntax, you want to restrict yourself to latin characters and digits. ----- '''Program # 3: Very similar to # 2. But this time the variable will be a character or a series of characters (ex: a name)''' [FW]: Not applicable, strings and ints are the same in Tcl. If you want to confirm the input was in integer form in the first example you can use ''[[string is int]]''. [LV] Note that the ''requirements'' probably should read "this time the variable will hold a character...". [FW]: Note that Tcl would do ''exit 0'' automatically, unlike C, which would issue a warning without an explicit return. So in practice they would be left out, LV or put them in to make for a better comparison. ----- '''What sdtin stands for''' [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. ---- '''Program # 4: We ask a person his/her age. If it is under 20, we 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" } [[This is of course missing a load of error handling, right now all errors just cause a cryptic syntax error exception during execution.]] [RS]: Alternatively, one could write for the last 5 lines: puts "You are [expr {$Age<20? {a child or a teen-ager}: {an adult now}}]" [Robert Abitbol] You would include all the ifs, else and put statements in one single statement. Very practical! : would mean "else" '''In a smaller number of lines''' 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}}]" ---- '''Program # 5: Very similar to program # 4 but a little more complex. We ask a person his/her age.''' * If it is under 12, we reply: "you are a child". * If it is between 13 and 19 we reply: "you are a teenager". * Else we reply: "you are an adult now".''' puts "Hey dude, how old might you be?" gets stdin Age if ... puts "You are a child" if ... puts "You are a teen" } else { puts "You are an adult now" '''In one statement''' 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}}]" ----- See also [Endekalogue], [Tcl cheat sheet] and [99 bottles of beer].