100% found this document useful (2 votes)
2K views3 pages

TCL Cheat Sheet

This document provides a cheat sheet for the Tcl programming language. It summarizes key concepts like code structure, data types, control flow statements, mathematical and string functions, I/O operations, and working with arrays. The cheat sheet also lists common commands for manipulating lists and strings. It is intended as a quick reference for Tcl version 8.7 and provides the essential syntax and functionality for core language features.

Uploaded by

Frost Burner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views3 pages

TCL Cheat Sheet

This document provides a cheat sheet for the Tcl programming language. It summarizes key concepts like code structure, data types, control flow statements, mathematical and string functions, I/O operations, and working with arrays. The cheat sheet also lists common commands for manipulating lists and strings. It is intended as a quick reference for Tcl version 8.7 and provides the essential syntax and functionality for core language features.

Uploaded by

Frost Burner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tcl cheat sheet

gets stdin x floor() fmod() hypot() int() log()


To print something on terminal: log10() pow() rand() round() sin()
puts "Hello World" sinh() sqrt() srand() tan() tanh()
Code Structure: The basic code structure revolves wide()
To print a variable:
around commands and scripts. Commands represent
puts $x
actions to be performed, and scripts are sequences of Ternary Operator:
commands. Procedures can be defined to create reusable Special Variables set max [expr {5 > 2 ? 5 : 2}]
code blocks, and control structures like conditionals and tcl_version Current version of Tcl
loops help manage program flow. Line breaks and Control Statements
interpreter
semicolons separate statements. Number sign (#) is used argc Conditionals
Number of arguments
to write comments. if-else:
passed to a script
argv List of the arguments if {$x < $y} {
Installation Guide puts $x
argv0 Name of the script
For windows, Install Git Bash for Windows. } elseif {$x == $y} {
tcl_precision Number of digits to retain
For Debian and Ubuntu Based Linux, puts "equal"
when converting floating-
$ sudo apt install tcl } else {
point numbers to strings
puts $y
RHEL, Fedora, and CentOS based distributions,
}
$ sudo yum install tcl Numerical Operations switch-case:
To use arrow keys for command history and other
To evaluate a mathematical equation expr command is switch $grade {
navigations, install rlwrap.
used. A {
$ sudo apt/yum install rlwrap puts "Excellent!"
expr {1+2} # returns 3
Add this line to ~/.bashrc: }
Mathematical Operators:
alias tclsh='rlwrap tclsh' F {
+ Addition Operator
puts "Try Again!"
Data Types - Subtraction Operator
}
* Multiplication Operator
Variables default {
/ Divisional Operator
int set x 5 puts "Invalid Grade!"
% Modulus Operator
float set x 5.5 }
Relational and Conditional Operators:
string set x "Hello World" }
== != > < Relational Operators
list set color {red green blue}
>= <=
array set user(Name) "Arnob" << >> Bitwise left and right shift
& ^ | ~ Bitwise AND, XOR, OR, NOT This cheat sheet refers to TCL 8.7:
IO Operation && || ! Logical AND, OR, NOT https://www.tcl.tk/man/tcl8.7/
To take input from user: Math Functions:
set x [gets stdin] abs() acos() asin() atan() atan2() Created by Arnob Karmokar
or, ceil() cos() cosh() double() exp() https://arnob.me
Loops List Commands: %f Floating point representation
For Loop: Command Output %e Floating point representation with
for {set i 0} {$i < 5} {incr i} { llength $fruits 3 mantissa-exponent form
puts $i lindex $fruits 1 banana %x Hexa-decimal representation
}
While Loop: lappend fruits apple banana Arrays
“berry” orange berry
set i 0 Array assignment and printing:
lsearch $fruits 2
while {$i < 5} { set languages(0) "Tcl"
“orange”
puts $i set languages(1) "Python"
lsort $fruits apple banana
incr i orange puts $languages(0)
} linsert $fruits 2 apple banana To print whole array:
or, “grape” grape orange parray languages
set i 0 lset $fruits 1 grape banana To get array size:
while {1} { “grape” orange
puts [array size languages]
if {$i >= 5} { lrange $fruits 0 apple banana
break 1
lreplace $fruits grape banana Associative Arrays:
}
0 0 “grape” orange set wizard(Name) "Harry"
puts $i
set wizard(House) “Gryffindor”
incr i String Manipulation set wizard(Patronus) “Stag”
}
Printing a string:
Foreach Loop:
set myVar "hello world" To get available indices:
set colors {red green blue}
puts $myVar puts [array names wizard]
foreach item $colors {
# returns {House Name Patronus}
puts $item
String Commands: **Iterate using foreach loop to access every item in the
}
equal, reverse, match, is, length, array.
List Manipulation compare, first, last, index, range, Other Array Commands:
toupper, tolower, replace, trim, map array set, array get, array exists,
List assignment and printing:
set fruits {apple banana orange} array insert
Usage:
or,
string tolower "Arnob" Dictionary
set fruits [list apple banana orange]
# returns arnob To create a dictionary:
or,
set fruits [split "apple banana dict set dictName key value
String Formatting: or,
orange" " "]
puts [format "%d" 4.5] dict create dictName key1 value1 key2
puts $fruits value2
%s String representation Example,
%d Integer representation dict create user first_name “Arnob”
dict set user last_name “Karmokar” Different access modes: extension, file isfile, file link, file
To get value from a dictionary: Mode Description lstat, file mkdir, file mtime, file
puts $user r Opens an existing text file for reading nativename, file pathtype, file
dict get $user “first_name” readable, file readlink, file rename,
purpose.
To get dictionary size: file size, file split, file tail, file
w Opens a text file for writing, if it does not
puts [dict size $user] type
exist, then a new file is created else existing
To get all available keys in a dictionary: file is truncated. Error Handling
[dict keys $user] a Opens an existing text file for writing in
Basic “try...on” error handling code structure:
To get all values in a dictionary: appending mode.
try {
[dict values $user] r+ Opens an existing text file for reading and
puts "Hello World"
To check if key exists in a dictionary: writing both. } on error {errorMessage} {
[dict exists $user “last_name”] w+ Opens a text file for reading and writing puts $errorMessage
both. It first truncates the file to zero length
Procedures if it exists otherwise create the file if it does
}

The syntax of creating a simple procedure: not exist. Regular Expressions


proc procedureName {arguments} { a+ Opens a text file for reading and writing Basic regular expression matching structure:
body both. It creates the file if it does not exist. regexp ?switches? exp string
} The reading will start from the beginning, ?matchVar? ?subMatchVar subMatchVar
Procedures with Multiple Arguments: but writing can only be appended.
proc multiply {a b} {
Example,
return [expr $a*$b]
Closing a File: regexp {c.*g} "abcdefghi" matched
}
close $f puts $matched
puts [multiply 3 5]
Writing to a File: # output==> cdefg
Procedures with Default Arguments:
proc multiply {a {b 10}} { puts $f "Hello World"
Basic regular expression substitution structure:
return [expr $a*$b] Reading a File:
regsub ?switches? exp string subSpec
} set file_data [read $f]
?varName?
puts [multiply 3 5] Reading a file line by line:
puts [multiply 3] set f [open "log.txt" r]
Example,
while {[gets $f line] >= 0} {
File Manipulation puts $line
regsub -all {\<foo\>} $string bar
string
Opening Files: }
open fileName accessMode close $f Other Important Tcl Commands
Other File commands:
clock, info exists, error, catch,
Example, eof, flush, seek, tell, file
global, upvar
set f [open "log.txt" r] attributes, file copy, file delete,
file executable, file exists, file

You might also like