Otcl Programming Basics Syntax
Otcl Programming Basics Syntax
Otcl Programming Basics Syntax
# ------------------------first---------------------#
#set a value to a variable:"set a 1"note in Tcl,variables are not
typed.The value assigned to a variable determines the type of
the variable.
set a 1
# declare a variable named "a" with a value "1"
# warning dont use captial S alphabet for "set" keyword
puts $a
# print a value
#----------------------second-----------------------#
# To use the value assigned to a variable ,we need to use the $
sign:set x $a
set x $a
# declare a variable named "x" with a value equal to the value
of the variable "a"
puts $x
#gedit : if c variable is not set (i.e set c $c is not present)
#terminal displays: can't read "c": no such variable
# # --> write a comment
#----------------------third-----------------------#
#A mathematical operation is done using the expression
command expr as follows: set b [expr $x +-*/% $a]
#declare a variable named "b" with a value equal to the sum of
"x" and "a"
set b [expr $x + $a]
puts "add a with b and the addition b : $b"
# Puts "text" print out the word "text"
# if x Puts "The value of x is $x" -> print out "The value of x is
0"
set b [expr $x - $a]
puts "Substract a from x and the substraction b : $b"
#---------------------fourth------------------------#
# To comment a line,use the symbol #
puts "comment a line use # followed by statements"
#--------------------fifth--------------------------#
#To create a le, we need to give it a name that will identify it
by our system. Then we need to assign a pointer to it that will
be used within the Tcl program in order to relate it:
set tclFilePointer [open ourFile.txt w]
# create a file, say "filename" or "ourFile" or "out1" ... and
assign a pointer to it within tcl program in order to relate it
"tclFilePointer" or "file1" ...
#--------------------sixth--------------------------#
# The command puts is used for printing an output. Note:
each time the "puts" command is used, a new line is started.
# To avoid new line, one has to add -nonewline after the "puts"
command.
#If we want to print into a file (say the one we defined above)
we type
# puts $filel "text"
# Tabulating is done by inserting \t.
#To print an output, use the command puts "". For instance, to
print into our File.txt the previous variable b with its tabulated
value, use the following line:
#puts $tclFilePointer "b \t a"
#puts $tclFilePointer "b \t $a"
puts $tclFilePointer "$a \t $x"
set a [expr $a+1]
set b [expr $b+2]
puts $tclFilePointer "$a \t $x"
set a [expr $a+1]
set b [expr $b+2]
#-------------------seventh-------------------------#
Note that when testing equality, we should use "==" and (not
"=" ie., The inequality is written with !=).
#------------nineth-------
} else {
puts "$a is a prime number"
}
}
}
prime $j
Ns2 programming
#-->--> Create a scheduler
#- set ns [new Simulator]
set ns [new Simulator]
# i.e.,
(0)
# i.e., (1)
(2)
# i.e.,
(0)
# i.e., (2)
(1)
(0)
# i.e.,
/ \
(0)---(1)
# i.e.,
\ /
# i.e.,
(2)
Src-agent.......................Dest-agent
(UDP,TCP)
(Null,LossMonitor,TCPSINK)
# Application
# (CBR,FTP, HTTP(Hypertext Transfer Protocol),
# SMTP(Simple Mail Transfer Protocol for email)
# TELNET(for remote virtual terminal)
# Domain Name Service (DNS for mapping comprehensible
# host names to their network addresses)
# File Transfer Protocol (FTP for file transfer))
# Define an application on the top of the src-agent
#--> Telnet
# set src [new Application/Telnet]
# $src attach-agent $Src-agent
#--> CBR
# set src [new Application/Traffic/CBR]
# $src attach-agent $Src-agent
proc finish {} {
global ns namfile
$ns flush-trace
close $namfile
exec nam results/versuch1.nam &
exit 0
}