Categories
tcl

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 !

Categories
tcl Tk

2nd Update : My First Tcl/Tk Application

Tonight i just want to implement searching note by keyword, and its done. Download source code below.

notes_update2

May be this will be the final update for the application.

Execute it using wish not tclsh …

Categories
tcl

Tcl : Working with array

Lets start with running tcl script below to try out some array command :

set ar(1) one
set ar(2) two
set ar(3) three

puts array_size:[array size ar]
puts array_get:[array get ar]
puts array_names:[array names ar]

foreach {key} [array names ar] {set x $ar($key); puts $x}


On example above we use 3 array subcommand :

Categories
tcl

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!

Categories
tcl

Tcl : While command

Basic syntax for while command in tcl language is

while test body

Tcl also have break and continue command as found on other language to control looping. Break within body will exit the loop. Continue will stop executing body and reevaluate the test.

Consider below sample :

Categories
tcl

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 :

Categories
tcl

Tcl : foreach command

Foreach command used to execute tcl scripts for every element of one or more lists. The basic syntax is below :

foreach varname list script

To iterate on a list and show element value on screen :

Categories
tcl

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.

Categories
tcl

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.

Categories
tcl

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.