Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Constant Declaration

This document provides an overview of the Tcl scripting language. It discusses: 1. The basic structure of Tcl scripts and how to execute them using tclsh or wish shells. 2. Common Tcl commands like puts, expr, and set for output, arithmetic, and variable operations. 3. Control structures like if/else, for loops, and procedures. 4. Built-in Tcl data types like strings, lists, and arrays and how to manipulate them. The document serves as a general introduction to Tcl syntax, commands, and programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Constant Declaration

This document provides an overview of the Tcl scripting language. It discusses: 1. The basic structure of Tcl scripts and how to execute them using tclsh or wish shells. 2. Common Tcl commands like puts, expr, and set for output, arithmetic, and variable operations. 3. Control structures like if/else, for loops, and procedures. 4. Built-in Tcl data types like strings, lists, and arrays and how to manipulate them. The document serves as a general introduction to Tcl syntax, commands, and programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#!

/usr/bin/tclsh

puts hello,world!

There are 2 types of shells we use in TCL:-

1. TCL Shell (tclsh)


2. Window Shell (wish) and to execute these we use ./name.tcl.

# is used in TCL/TK for comments.

For arithmetic operations we use:-

puts[expr 3+2]; #sum of 3+2 is 5

Constant Declaration:-

puts [expr 1+6+9]

Variable Substitution:-

set a 3;

puts $a;

FOR NEXT LINE

#!/usr/bin/tclsh

puts Hello \n World;

Output will be:-

Hello

World

Data Types:-

set myvariable 18

puts $myvariable

Output:- 18

puts [expr $myvariable+6+9]


Output:-33
We can use , &, {} for the strings that we want to print i.e.

set myvariable helloworld


set myvariable {hello world}
Output will be same.

List:-

We can use or []

set myvar {red green blue}


puts [lindex $myvar 2]
set myvar red green blue
puts [lindex $myvar 1]
lindex (L INDEX) is used for accessing list elements using index.

Array:-

set marks (English) 80


puts $marks (English)
set marks (maths) 50
puts $marks (maths)
Output will be:-
80
50

Handles:-

set myfile [open file name]

Variables:-

set variable name value

For ex:-

set variable A 10
set [variable B] test
puts ${variable B}

Dynamic Type:-

set variable 10
puts $variable A

set sum [expr $variable A+20]

Default precision of TCL is 12 digits.

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:-

set arrayname (index) value

set languages (0) TCL

set languages (1) C Language

puts $languages(0)

puts $languages(1)

Output will be:-

TCL

C Language

Size of Array:-

[array size variable name]


set languages(0) TCL
set languages(1) C Language
puts[array size languages]

Array Iteration:-

set languages(0) TCL


set languages(1) C Language
for {set index 0} {index < [array]} {incr index} {
puts languages ($index: $languages)
}

Associative Arrays:-

set personA (name) vivek


set personA (age) 30
puts $personA (name)
puts $personA (age)

Indices of array:-

[array name variable name]


set personA(name) Anjana
set personA(age) 28
puts [array name personA]
Output:- 28 Anjana

Iteration of Associative Array:-

set personA(name) virat


set personA(age) 27
forach index [array name personA]
{
puts personA ($index): $personA ($index)
}

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:-

set list name {items 1 2 3 4}

OR

set list name {list 1 2 3 4}

OR
set list name {split item 1_2_3_4}

Things to be operated on it:-

1. append list name split_character value


OR
lappend list name value
For ex:- set var blue
append var red blue
lappend vargreen
2. length set var {sachin tendulkar}
puts [length $var]
3. lindex $var index
Value of index:- 0,1------n

Insert:-

set var [linsert $var blue]

puts $var

Replace:-

set var [lreplace $var 2 3]

puts $var

Set item at index:-

set var {virat kohli}

lset var 0 ms dhoni

puts $var

So, whatever be at 0 position, it will be replaced by lset command and set as the
value written with lset command.

Transform list to variables:-

set var {---}

lassign $var column1 column2

puts $column1
puts $column2

lsort list name

set var []

set var [lsort $var]

puts $var

Procedures:-

proc hello world {} {

puts hello,world!

Procedures with multiple origins:-

proc add {a b} {

return [expr $a+$b]

puts [add 10 30]

Procedures with variable arguments:-

proc avg [members]

set sum 0

forach member $members {

set sum [expr $sum + $member]

set average [expr $sum/[no. of members]]

return $average

puts {avg[70 80 60 50]}


Procedures with default arguments:-

proc add {a{b 100} {

return [expr $a + $b]

puts [add 10 30]

puts [add 10]

Output:- 40

110; If b is matching a value, the default provided value will be used.

Procedures and buttons:-

How to pick a variable from other variable?


If one variable is global, we can use it anywhere inside a main program.
We can use astring in case of text variable.
label.lab- text variable
text var:- If it is global then only .lab can access data through text
variable.
If a counter is declared as global, then its value changes according to
counter at any point of the program and its value is reflected in in main
part of program.
button.but- text variable textvar2-text
click me1-command click-button
pack.lab.button
In the main program, we give all the related work using GUI & buttons.
text variable textvar has more priority than text $counter.

There are various methods to pass values to variable:-

1. We can call by using name only with no $ sign required.


2. To manage multiple arguments we use {args}
-text or label both can be used to display text.
To create a list box in TCL/TK we use the command listbox.
Insert is used without a minus(-) sign.
Curselection is used for cursor selection from declared list from which
we select the indices of the list.
== .. -> This string is empty or not selected.
For menu we have menu function inbuilt in tk.
menu .mbar
.config menu.mbar -> Configuration of menu bar
Various arguments of tk_manage box:-
-type for Yes, No or Ok
tk_choose directory -> It helps to capture indices
-font ansi 12 bold -> No change in order can be possible.
Nested procedures can be possible in tcl/tk.
Main procedure will call the inside procedure by default.
For top level, we have toplevel command.
.top.but -> for top level, if we want to add but we use top level with
button variables.

You might also like