100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Tcl Quick Reference

A fast-lookup cheat sheet covering core Tcl syntax, list/string commands, and the essential Tk widget and geometry commands.

PracticeBeginner7 min readJul 10, 2026
Analogies

Tcl Quick Reference

This reference collects the core Tcl syntax and command families a working developer reaches for daily: variable and command substitution, control structures, list and string manipulation, and the essential Tk widget/geometry commands, all summarized for quick lookup rather than deep explanation.

🏏

Cricket analogy: It's like a pocket-sized fielding-positions chart a young captain keeps in their pocket — not a full coaching manual, just the essential reference to glance at mid-over when setting a field quickly.

Core Syntax: Variables, Substitution, Control Flow

Variables are set with set name value and read with $name; command substitution nests a command's result into another with square brackets, set total [expr {$a + $b}]; and control flow uses if {cond} {...} elseif {cond2} {...} else {...}, while {cond} {...}, for {init} {cond} {incr} {...}, and foreach item $list {...} — each keyword is itself just an ordinary command, so if, while, and foreach all accept braces as their block arguments the same way any other command accepts a string argument.

🏏

Cricket analogy: It's like every match official — the umpire, the third umpire, the match referee — technically holding the same 'official' status under the laws of cricket, even though their duties differ; in Tcl, if, while, and foreach are all just 'commands' under the hood, no matter how special they look.

tcl
# Variables & substitution
set name "Ada"
puts "Hello, $name"
set total [expr {2 + 3}]

# Control flow
if {$total > 4} {
    puts "big"
} elseif {$total == 4} {
    puts "equal"
} else {
    puts "small"
}
foreach item {a b c} { puts $item }

# Lists & strings
set nums [list 5 3 9 1]
set sorted [lsort -integer $nums]
set csv "a,b,c"
set fields [split $csv ","]
puts [join $fields "-"]

# Tk widgets & geometry
button .b -text "OK" -command {puts ok}
pack .b -side top -fill x
grid .b -row 0 -column 0 -sticky nsew

Lists, Strings, and Regular Expressions

For sequences, lappend, lindex, llength, lsort, and lsearch cover the most common needs, while split/join convert between strings and lists (split $csvLine "," then join $fields "-"). For raw text, string length, string range, string map, and string trim handle the bulk of everyday manipulation, and regular-expression work goes through regexp/regsub — for example, regexp {^\d+$} $input tests whether a string is purely numeric.

🏏

Cricket analogy: It's like a scorer's toolkit: a running tally (lappend), looking up a specific ball's outcome (lindex), counting total deliveries bowled (llength), and re-sorting a batting averages table (lsort) — each command is a distinct scorer's tool for a distinct job.

Tcl command abbreviation is allowed only when unambiguous and is a REPL convenience, not something to rely on in scripts — always write full command and subcommand names (string length, not string l) in production code for clarity and forward compatibility.

Tk Widgets and Geometry Managers

The widgets reached for most often are label, entry, button, frame, text, and canvas, each created as widgetType .path -option value ...; the three geometry managers are pack .w -side top|left|right|bottom -fill x|y|both, grid .w -row N -column N -sticky nsew, and place .w -x N -y N. Combined with bind .w <Event> {script} for interaction and wm title . / wm geometry . for window-level control, these commands cover the vast majority of everyday Tk layout work.

🏏

Cricket analogy: It's like a coach's essential kit bag: stumps, bails, a bat, gloves, and pads — a handful of core items (widgets) that, combined with a few standard drills (geometry managers), cover the vast majority of a training session's needs.

split and join are inverses only when your delimiter can't appear inside the data itself — splitting a CSV line naively on , breaks for quoted fields containing commas; for real CSV parsing, use the csv package from Tcllib instead of hand-rolled split.

  • set/$ handle variable assignment and substitution; [...] performs command substitution nested inside another command.
  • if/elseif/else, while, for, and foreach are ordinary Tcl commands that happen to take braced blocks as arguments.
  • List commands (lappend, lindex, llength, lsort, lsearch) and split/join cover most sequence manipulation needs.
  • String commands (string length, string range, string map, string trim) and regexp/regsub cover most text processing needs.
  • Core Tk widgets are label, entry, button, frame, text, and canvas; each is created with widgetType .path -option value.
  • The three geometry managers are pack, grid, and place, each suited to a different layout style.
  • bind handles interaction and wm title/wm geometry control window-level properties.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#TclTkStudyNotes#TclQuickReference#Tcl#Quick#Reference#Core#StudyNotes#SkillVeris#ExamPrep