Tcl Learning Notes

Programming | 09 October 2017

In this page, you will find the commonly used syntax and how to’s that is specific to Tcl programming language. If you know what Tcl is, but wanted to quickly refer to Tcl syntax, then you might find this page useful. I will keep on updating this article. So, kindly bookmark this page if you find it useful :)

Standard IOcode

1
2
3
4
5
# get user input
set a [gets stdin]

# print output to screen
puts $a

Math operationscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# basic math operations
set add [expr $a + $b]
set sub [expr $a - $b]
set mul [expr $a * $b]
set div [expr $a / $b]
set mod [expr $a % $b]

# some more math operations
puts "[expr sqrt(4)]"
puts "[expr sin(4)]"
puts "[expr cos(4)]"
puts "[expr tan(4)]"
puts "[expr sinh(4)]"
puts "[expr cosh(4)]"
puts "[expr tanh(4)]"
puts "[expr log(1)]"
puts "[expr ceil(4.6)]"
puts "[expr floor(4.6)]"
puts "[expr srand(4.6)]"

Looping statementscode

1
2
3
4
5
6
7
8
9
10
11
# for loop
for {set i 0} {$i < 50} {incr i} {
	puts $i
}

# while loop
set i 0
while {$i < 50} {
	puts $i
	incr i
} 

Conditional statementscode

1
2
3
4
5
6
7
8
set i 50 
if {$i > 100} {
	puts "Ferrari"
} elseif {$i > 50} {
	puts "Mclaren"
} else {
	puts "Pikachu"
}

Logical operationscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set i "Batman"
set j 2

# logical AND & Logical OR
if {$i == "Ironman" && $j == 1} {
	puts "I love Ironman"
} elseif {$i == "Batman" || $j == 2} {
	puts "I love Batman"
}

# boolean
set a true
if $a {
	puts "Yes! It is true"
}

Switchcode

1
2
3
4
5
6
7
8
9
10
11
12
set x 30

# the best syntax to use "switch" in Tcl
# although there are multiple ways
# such as writing in a single-line
switch $x {
  "10" {puts "this is 10"}
  "20" {puts "this is 20"}
  "default" {puts "this is default"}
}

# prints "this is default"

Listscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Three ways to create a list
set l {"Dhoni" "Kohli" "Dravid"}
set l [list Dhoni Kohli Dravid]
set l [split "Dhoni Kohli Dravid"]

# length of a list
puts "[llength $l]"

# get the item at a specific index
puts "[lindex $l 2]"

# retrieve several items within a range
puts "[lrange $l 0 2]"

# concat two lists
set l2 {Ganguly Sehwag}
set l [concat $l $l2]

# append an item to list
lappend l Rohit

# insert an item to a particular index
set l [linsert $l 2 "Dhawan"]

# replacing an item in the list 
# Note that we need to give here a range
set l [lreplace $l 2 3 "Laxman"]

# search for an item in the list
# returns index of the item if there is match
# else returns -1
set check [lsearch $l "Dhoni"]

# sorting a list (increasing order)
puts "[lsort $l]"

# sorting a list (decreasing order)
puts "[lsort -decreasing $l]"

Dictionariescode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# create a dictionary with key-value pair
set d [dict create 1 Dhoni 2 Kohli 3 Rohit]

# get the unique keys in the dictionary
set keys [dict keys $d]

# loop over each key to get its corresponding value
foreach key $keys {
	# get the value of a key
	set value [dict get $d $key]
	puts "$key -- $value"
}

# prints the size of dictionary
puts "[dict size $d]"

# prints the values in the dictionary
puts "[dict values $d]"

Stringscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
set s "/home/gogul/software/sublime/packages"

# get the length of the string
puts "[string length $s]"

# get the character at a specified index in the string
puts "[string index $s 2]"

# get the characters within a range in the string (sub-string)
puts "[string range $s 0 10]"

# split the string and make it a list 
# observe the delimiter here
set l [split $s /]

# compare two strings (not numerically)
# -1 --> if string1 is less than string2
# 0  --> if string1 is equal to string2
# 1  --> if string1 is greater than string2
set s1 "Dhoni"
set s2 "Kohli"
puts "[string compare $s1 $s2]"

# match a pattern in the string
puts "[string match "*gogul*" $s]"

# convert all letters to upper-case
puts "[string toupper $s]"

# convert all letters to lower-case
puts "[string tolower $s]"

# convert all letters to title 
# first letter upper-case followed by lower-case
puts "[string totitle $s]"

Regular expressionscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
set s "This is a string with a number 123456789"

# regex to match a number
set result [regexp {\d.*\d} $s match]

# returns 1 if there is a match
puts $result

# returns the matched string
puts $match

# search for a pattern and if present,
# substitute it with a string
# and return the result
set s "Dhoni is an awesome player in cricket."

regsub Dhoni $s Kohli result

# prints Kohli is an awesome player in cricket.
puts $result

Procscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# a simple proc with two arguments and a return statement
proc addition {a b} {
	set c [expr $a + $b]
	return $c
}

puts "[addition 5 4]"

# proc with multiple arguments
# "args" as the third argument holds all additional values supplied
proc more_args {a b args} {
	puts $a 
	puts $b
	puts $args
}

# prints 
# 5
# 6
# 90 60 10
more_args 5 6 90 60 10

File handlingcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# opens a file to write
set fp [open "sample_text_file" w]

# writes data to the file
puts $fp "Hello World"

# close the file after writing (else data won't be saved)
close $fp


# open a file to read
set fp [open "sample_text_file" r]

# read the content of the file
set fp_data [read $fp]


# split by newline
set lines [split $fp_data "\n"]

# loop through each line in the file
foreach line $lines {
	# do some processing
}

Utilscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# execute shell commands inside Tcl
# prints the present working directory
exec pwd

# prints the current datetime
exec date

# creates a new directory in the present working directory
exec mkdir new_folder

# save the pwd in a variable 
set p [pwd]

# change directory
cd another_folder

Otherscode

1
2
3
4
# execute a file within a file
# so that procs/variables used in another file
# are available in the current file
source filename


In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.