Constant Declaration
Constant Declaration
/usr/bin/tclsh
puts hello,world!
Constant Declaration:-
Variable Substitution:-
set a 3;
puts $a;
#!/usr/bin/tclsh
Hello
World
Data Types:-
set myvariable 18
puts $myvariable
Output:- 18
List:-
We can use or []
Array:-
Handles:-
Variables:-
For ex:-
set variable A 10
set [variable B] test
puts ${variable B}
Dynamic Type:-
set variable 10
puts $variable A
set variable 10
set result [expr $variable A/9.0];
set variable 10.0
set variable 10
set tcl_precision 5
set result [expr $variable A/9.0];
Operators:-
1. Arithmetic:- +,-,*,/,%
set a 5
set b 8
set c[expr $a+$b]
2. Relational:- ==,!=,>,<.>=.<=
set a 21
set b 10
if {$a == $b} {
puts statement
}
else {
puts Line 1
}
3. Logical Operators:- &&,||,!
if {$a && $b} {
puts True
}
4. Bitwise Operators:- &,|,^,<<,>>
set c [expr $a <<2]
5. Ternary Operators:- ?:
set b [expr $a==10?20:30]
Decisions:-
1. If:-
if {Boolean_exp} {
#statements
}
2. If else:-
if {$a<20} {
puts tcl
}
else {
puts tk
}
3. Else If:-
if {$a ==10} {
puts st1
}
elsif {$a ==20} {
puts st2
}
else {
puts st3
}
4. Nested If:-
if {$a ==100} {
if {$b ==100} {
puts xyz
}
}
5. Switch:-
switch $grade {
A{
puts ab
}
B{
puts cd
}
C{
puts ef
}
default {
puts yz
}
}
6. Nested Switch:-
switch switch string {
match string 1 {
body
switch switch string {
match switch string {
match string 1 {
body 1
}
match string 2 {
body 2
}
match string n {
body n
}
}
}
match string 2 {
body 2
}
}}
Loops:-
1. While Loop:-
set a 10
while {$a <20} {
puts value: $a
incr a
}
2. For Loop:-
for {set a 10} {$a <20} {incr a}
{
puts $a
}
3. Nested For:-
for {set i 2} {$i <100} {incr i}
{
for {set j 2} {j <= [expr $i/$j]} {incr j} {
if {[expr $i%$j==0]} {
break
}
}
if {$j>[expr $i/$j]} {
puts $i
}
}
Arrays:-
puts $languages(0)
puts $languages(1)
TCL
C Language
Size of Array:-
Array Iteration:-
Associative Arrays:-
Indices of array:-
Strings:-
set S1 dav
set S2 id
puts [string compare S1 S2]
if {} {
set S1 great
set S2 0
}
puts First occurrence of $S2 in S1
puts {string first $S2 $S1}
puts character at index 0 in S1
puts {sting index $S1 0}
puts {string first $S2 $S1}
puts {string last $S2 $S1}
word end
word start
length $S1
toupper $S1
tolower $S1
Trim right $S1
Trim $S1 $S2
latch .abc $S1
Lists:-
OR
OR
set list name {split item 1_2_3_4}
Insert:-
puts $var
Replace:-
puts $var
puts $var
So, whatever be at 0 position, it will be replaced by lset command and set as the
value written with lset command.
puts $column1
puts $column2
set var []
puts $var
Procedures:-
puts hello,world!
proc add {a b} {
set sum 0
return $average
Output:- 40