Tcl catch command

Catch command used to execute a script and handle if any error happen. Catch command will return 0 on success and 1 on error. Catch command will give error message to a variable. 

For examples we will execute expr command to raise divide by zero error to examine what a catch command will do.

Here we can see on error catch will return 1 and error message variable will be filled.

We can also use catch to simply ignore error on executed script !

Tcl : Creating Array

Tcl array can be created by set command by specifying its key to array name.

From example above we define array named days with keys containing numbers and text values. If we try to do variable substitution with $days, it wont work. If we do variable substitution with key specified then it will work.

We can have text as array’s key. Tcl array is an associative array.

From sample above we know that a text key is case sensitive (ID is different with id).

Another way to create array is with array set command by passing list of key value pairs as 3rd parameter and array name as 2nd parameter.

Thats all how we create array in Tcl. Bye!

Tcl : for command

For command used on tcl to create looping with syntax below :

for start test next command

for command have 4 parameter. The first parameter is start block where we set an initial value to a variable (usually using set command). Second parameter is test block where we test a variable or condition(will be tested using expr command). The third parameter is next block which usually used to change variable value (can use incr command). The fourth parameter is command which will be executed in loop.

Basic example below , using i as variable :

Continue reading “Tcl : for command”

Tcl lsort command

lsort is a command to return a sorted list based on option. Default option is to sort alphabetically (-ascii) and ascending (-increasing). lsort can also sort numerically using -integer or -real option. lsort can also sort based on a element on sub list using -index option.

There are more options available. Here we will see some examples of lsort in action.

Tcl Octalogue

Octalogue is eight rules that describe Tcl language as found from https://wiki.tcl-lang.org/page/Dodekalogue.

Script
A script is composed of commands delimited by newlines and semicolons, and a command is composed of words delimited by whitespace.
Evaluation
After escapes and substitutions in each word are processed, words preceded by {*} are transformed into multiple words in the command, the first word of each command is used to locate a routine, and that routine is called with the remaining words as its arguments.

Continue reading “Tcl Octalogue”

Tcl Expr command

All in tcl is string, so expr exist as command to evaluate numeric value. On other language like ruby if we write 1+2 <enter> on the irb console will print 3, but thats not the case with tcl.

To get numeric calculation on Tcl we need to use expr command.

Evaluating logical also work with expr command. Examples below.

By remembering Tcl rule : “All is string”, we can conclude that expr result will be string as well.