Tcl command Reference : http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
Online Tcl script editor :http://www.compileonline.com/execute_tcl_online.php
To check the tcl output please copy paste the below code in online tcl script editor and for more detailed explanation use the above reference .
(1.)Simple Text Output :
puts "signal is not valid !!"
puts stdout "error !!"
(2.)Assigning values to variables :
set P "this is signal"
set Q 12
set R "1010101"
set S 18.881
set M $S
puts "P=$P , Q=$Q"
puts "bit vector is $R"
puts "S=$S and M=$M"
(3.) Newline ,tab and backspace
set P "\n \b this is signal\n"
puts "Tab\tTab\tTab"
puts "P=$P"
(4.) Grouping Statements using {}
set LABLE "this is a sample msg"
puts "$LABLE"
puts "$LABLE \t{$LABLE}"
(5.) Evaluation & Substitutions []
set X "abc"
set Z [set E "10101"]
puts "\n X=$X \n Z=$Z"
(6.) Results of a command: math functions,expr evaluation
set x 1
set p "this is signal"
set yy [expr { $x>0? ($x+1) : ($x-1) }]
set tt [expr { [string length $p]-2*$x }]
set pi6 [expr {3.1415926/6.0}]
set range pow(2,6)
puts "\n $yy \n $tt"
puts "sin($pi6)=[expr {sin($pi6)}]"
puts "range is $range"
(7.) Arithmetic operations
Division :
puts "1/2 is [expr {1/2}]"
puts "-1/2 is [expr {-1/2}]"
puts "1/2 is [expr {1./2}]" # . -> floating point rep.
puts "1/3 is [expr {1./3}]" # . -> floating point rep.
puts "1/3 is [expr {double(1)/3}]"
(8.) Comparisons
if-else :
set X 1
if {$X != 1} {
puts " X is not 1"
} else {
puts "X is 1"
}
else-if :
set ii 2
set k "this is else-if clause"
if { $ii == 1} {
puts "1.k=$k"
} elseif { $ii == 2} {
puts "2.k=$k"
} else {
puts "3.k=$k"
}
(9.) Looping statements
while loop :
set x 1
while { $x < 8} {
set x [expr {$x + 1}]
if { $x == 3 } {
continue
} else {
puts "x =$x"
}
}
For loop :
example 1.
for {set i 0} {$i < 10} {incr i} { # increment by 1
puts "i is $i"
}
example 2.
for {set i 0} {$i < 10} {incr i 2} { # increment by 2
puts "i is $i"
}
example 3.
for {set i 0} {$i < 64 } {incr i} {
# generate all combination 6 bit binary number
# useful for random generation on inputs
puts "\n"
puts "[expr ($i & 0x20) >> 5] [expr ($i & 0x10) >> 4] [expr ($i &
0x08)>> 3] [expr ($i & 0x04) >> 2] [expr ($i & 0x02) >> 1] [expr $i
& 0x01]"
}
(10.) Arrays
example 1.
array set val [list a 1 b 2 c 39]
puts "$val(a)"
puts "$val(b)"
puts "$val(c)"
example 2.
for {set i 0} {$i < 5} {incr i} { set a($i) $i }
for {set i 0} {$i < 5} {incr i} { puts $a($i) }
example 3.
array set val [list 2 "enb_n" 1 "din_1" 0 "din_0"]
puts "$val(2)"
example 4.
array set CODE-LIST [list CODE-1 123 \
CODE-2 678 \
CODE-3 198 \
CODE-4 "django unchained" \
CODE-5 "The Matrix" ]
puts "$CODE-LIST(CODE-5)"
> The Matrix
puts "$CODE-LIST(CODE-1)"
> 123
(11.) Adding new commands using proc
proc add {arg1 arg2} {
set x [expr {$arg1 + $arg2}];
return $x
}
set A 1
set B 2
puts "A+B=[add $A $B]"
For Tcl tutorial Part-2 click here
No comments:
Post a Comment