Getting Started
4 snippetsBasic Tcl syntax and fundamentals
Hello World
puts "Hello, World!" Comments
# Single line comment
# Multi-line comments require
# multiple hash symbols Run Script
# Save as script.tcl
tclsh script.tcl Interactive Shell
tclsh
% puts "Interactive mode"
% exit Variables & Data Types
6 snippetsVariable declaration and manipulation
Variable Assignment
set name "John"
set age 30
set price 19.99 Variable Usage
puts $name
puts "Hello, $name"
puts "Age: $age" Variable Substitution
set x 10
set y 20
set sum [expr {$x + $y}]
puts "Sum: $sum" Unset Variable
set temp 100
unset temp
if {[info exists temp]} {
puts "Variable exists"
} Global Variables
proc myProc {} {
global counter
incr counter
}
set counter 0
myProc
puts $counter ;# 1 List Variables
set fruits {apple banana cherry}
set numbers [list 1 2 3 4 5] Operators & Expressions
5 snippetsArithmetic and comparison operators
Arithmetic
set a 10
set b 3
set sum [expr {$a + $b}]
set diff [expr {$a - $b}]
set prod [expr {$a * $b}]
set quot [expr {$a / $b}] Increment/Decrement
set count 0
incr count ;# count = 1
incr count 5 ;# count = 6
incr count -2 ;# count = 4 Comparison
expr {$a == $b} ;# Equal
expr {$a != $b} ;# Not equal
expr {$a > $b} ;# Greater
expr {$a < $b} ;# Less
expr {$a >= $b} ;# Greater or equal
expr {$a <= $b} ;# Less or equal Logical Operators
expr {$a && $b} ;# AND
expr {$a || $b} ;# OR
expr {!$a} ;# NOT String Comparison
set str1 "hello"
set str2 "world"
if {$str1 eq $str2} {
puts "Equal"
}
if {$str1 ne $str2} {
puts "Not equal"
} Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
5 snippetsConditional statements and branching
If Statement
if {$age >= 18} {
puts "Adult"
} If-Else
if {$score >= 60} {
puts "Pass"
} else {
puts "Fail"
} If-Elseif-Else
if {$grade >= 90} {
set result "A"
} elseif {$grade >= 80} {
set result "B"
} elseif {$grade >= 70} {
set result "C"
} else {
set result "F"
} Switch Statement
switch $day {
Monday {
puts "Start of week"
}
Friday {
puts "End of week"
}
default {
puts "Midweek"
}
} Switch with Pattern
switch -glob $filename {
*.txt {
puts "Text file"
}
*.log {
puts "Log file"
}
default {
puts "Other file"
}
} Loops
5 snippetsIteration constructs
For Loop
for {set i 0} {$i < 10} {incr i} {
puts $i
} While Loop
set count 0
while {$count < 5} {
puts $count
incr count
} Foreach Loop
set fruits {apple banana cherry}
foreach fruit $fruits {
puts $fruit
} Foreach with Multiple Lists
set names {John Jane Bob}
set ages {30 25 35}
foreach name $names age $ages {
puts "$name is $age years old"
} Break and Continue
for {set i 0} {$i < 10} {incr i} {
if {$i == 5} {
break
}
if {$i == 3} {
continue
}
puts $i
} Procedures
5 snippetsDefining and calling procedures
Basic Procedure
proc greet {name} {
puts "Hello, $name"
}
greet "World" Procedure with Return
proc add {a b} {
return [expr {$a + $b}]
}
set result [add 5 3]
puts $result ;# 8 Default Arguments
proc greet {name {greeting "Hello"}} {
puts "$greeting, $name"
}
greet "John" ;# Hello, John
greet "Jane" "Hi" ;# Hi, Jane Variable Arguments
proc sum {args} {
set total 0
foreach num $args {
set total [expr {$total + $num}]
}
return $total
}
puts [sum 1 2 3 4 5] ;# 15 Procedure with Upvar
proc increment {varname} {
upvar $varname var
incr var
}
set count 10
increment count
puts $count ;# 11 Lists
7 snippetsWorking with lists
Create Lists
set fruits {apple banana cherry}
set numbers [list 1 2 3 4 5]
set mixed {John 30 "New York"} List Length
set fruits {apple banana cherry}
set len [llength $fruits] ;# 3 Access Elements
set fruits {apple banana cherry}
set first [lindex $fruits 0] ;# apple
set last [lindex $fruits end] ;# cherry List Range
set numbers {1 2 3 4 5}
set subset [lrange $numbers 1 3] ;# {2 3 4} Append to List
set fruits {apple banana}
lappend fruits cherry orange
puts $fruits ;# {apple banana cherry orange} Search List
set fruits {apple banana cherry}
set index [lsearch $fruits banana] ;# 1
set found [lsearch $fruits grape] ;# -1 (not found) Sort List
set numbers {5 2 8 1 9}
set sorted [lsort -integer $numbers]
puts $sorted ;# {1 2 5 8 9} File I/O
6 snippetsReading and writing files
Write to File
set fp [open "output.txt" w]
puts $fp "Hello, World!"
close $fp Append to File
set fp [open "log.txt" a]
puts $fp "Log entry"
close $fp Read Entire File
set fp [open "input.txt" r]
set content [read $fp]
close $fp
puts $content Read Line by Line
set fp [open "data.txt" r]
while {[gets $fp line] >= 0} {
puts "Line: $line"
}
close $fp File Exists
if {[file exists "test.txt"]} {
puts "File exists"
} File Operations
file copy source.txt dest.txt
file delete temp.txt
file rename old.txt new.txt
file mkdir newdir