Skip to main content

Tcl tutorial for Begineers

Initially, we should get ns or tcl shell installed to work with tcl script. 
If you didn't install ns2, click here for the installation procedure. Else, follow the tutorial. 
After installing ns-2, type ns in terminal. Then, there appears % symbol, which symbolizes the invokement of tcl shell script.
Note(optional): TCL scripting individually can be practiced with in Linux system with tcl shell scripting.


Introduction: 
TCL is an abbreviation for Tool Command Language. Tcl is a string-based command (script) language, with simple syntax. Tcl is designed to be a glue that assembles software building blocks into applications.The latest version of TCL is 8.8.2. 

The goal of this tutorial is to introduce the learner to TCL scripting and make comfortable to do scripting required for ns2 network simulations. It covers working with variables, lists, arrays, control structures and other core features, in TCL. The tutorial was created on Ubuntu Linux, but the users can practice TCL scripting individually in WINDOWS by downloading ActiveTCL.


Working with TCL:

set – command to assign a value to a variable.

set variable_name value
set u 10

set variable_name2 $variable_name
set c $u

# - Symbol used for line comment. Tcl interpreter will not execute that line. There isn’t any paragraph commenter in tcl/otcl scripting.

expr – command to invoke any mathematical operation.
set variable_name [expr $var1+$var2]
set d [expr $b + $c]

puts – Command to print output to the console.
puts 10
puts d
puts $d
puts d is different from puts $d

In Tcl, there are no variable types. So, a variable can be a string or integer depending on value you assign to it. Also, the typecasting rules also apply here.
puts [expr 1/2]
puts [expr 1.0/2]
puts [expr 1/2.0]
puts [expr 1.0/2.0]

/  operator is used for division. \ operator is not defined in tcl. NOTE: spaces are not important
expr 5*3
expr 5    *     3

Both these statements, result the same answer. But, spaces are not preferable for good programming.

NOTE:
Dividing by zero
expr 1/0
Interpreter shows "DIVIDE BY ZERO" error.
This can't be done by a programming language

Working with strings, in TCL:

set e “udhay prakash”;
set f {udhayprakash.blogspot.in};
puts $e
puts “udhay prakash”
puts $f
puts “udhayprakash.blogspot.in”



Conditional Statements:
Syntax:
if {expression}
{          <execute some commands>
} else {
            <execute some commands>
}

Example:
set a 10;
set b 20;
if($a!=$b){
puts “$a!=$b”;
}else{
puts “$a=$b”;
}

Loop Statements:
for loop syntax:
for{initialization}{condition}{increment/decrement}
{ <execute some commands>
}

for loop example:
set i 0
for{set i 0}{$i<5}{incr i}{
puts “$i/t”
puts [expr $i/t]
}

while loop syntax:
while {condition}{
<execute some commands>
<increment/decrement looping variable>
}

while loop example:
set m 10
set n 2
while{$m!=0}{
puts {$m\n}
incr m -1
}

do-while loop syntax:
do{
<execute some commands>
<increment/decrement looping variable>
}while{condition};

do-while loop example:
set m 10
set n 2
do{
puts {$m\n}
incr m -1
}while{m};

return – command to return a value from procedure
continue – command to bypass a loop in a procedure
break – command to come out of a procedure
proc – command to create a procedure(function). The procedure receives some parameters that can be objects, files or variables.

Procedure syntax:
 proc procedure{in_par1, in_par2, …..}{
            global variable_list, file_list
            <execute some commands>
             return $something
            }
Procedure Example1:
proc factorial {j} {
for {set result 1} {$j > 1} {set j [expr $j ­1]} {
set result [expr $result * $j]
}
Procedure Example2:
#create a procedure
proc test{}{
set a 4
set b 2
set c [expr $a+$b]
set d [expr [expr $a-$b]*$c]
puts “c=$c, d=$d”
for {set k 0}{$k<10}{incr k}
if {$k<5}{
puts “k<5,pow=[expr pow($d,$k)]”
} else{
puts “k>=5,mod=[expr $d%$k]”
}
}}
Calling a Procedure:
Syntax: procedure_name
Example: test



Working with files, in TCL:
·         To create and write in a new file, with file1 as the filename: set file1 [open filename w]; 
·         To read an already existing file, with file2 as the filename:   set file2 [open filename r];
·         To print data into a file, with file2 as filename:                                   puts $file2 “text”;

Comments

Popular posts from this blog

NSG 2.1 Tcl/OTcl Script Generator

Are you a beginner for ns2 network simulator? Are you afraid of Tcl/oTcl script generation? Then there is a tcl script generator, named NSG 2.1 NSG - Network Simulation Generator NSG 2.1 is a java .jar file.  So, this application can run on all platforms (windows/linux/mac os). It deserves the java installed in you pc, prior to working with NSG 2.1. Java must be installed to run NSG2.1. So, initially, java must be installed. How to install java in Windows/ubuntu/mint/debian linux/ OS X ? Step 1 : Go to Terminal and run  java -version   to check the java version installed in your machine. Step 2 :  For Windows, click here  to download the java installer. Then, it is a typical next-next windows executable installation. For ubuntu/mint/debian linux operating systems, run the following commands in that terminal: sudo apt-get install default-jre sudo apt-get install default-jdk sudo apt-get install openjdk-7-jre sudo apt-get install openjdk-7- jdk How to

SUMO installation in linux (Debian/Ubuntu/Mint)

SUMO - S imulation of U rban Mo bility SUMO an open source, portal, microscopic, multi-modal road traffic simulation. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.  It allows to simulate as to how a given traffic demand which consists of single vehicles moves through a given road network. The simulator allows to address a large set of traffic management topics. It is purely microscopic: each vehicle is modeled explicitly, has an own route, and moves individually through the network. SUMO Installation Procedure: Installation in Linux and mac OS Step I: Install two pre-requisite packages to build SUMO with GUI. Go to terminal and run: $ sudo apt-get install libgdal1-dev proj libxerces-c2-dev $ sudo apt-get install libfox-1.6-dev libgl1-mesa-dev libglu1-mesa-dev Step II: If you are using Unbuntu 12.04 or older versions, as it doesn't ship with libgdal package, create a symbolic link: $ sudo ln -s /usr/lib/li

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len