+++
title = "Tcl"
author = ["Kaushal Modi"]
description = "Notes as I am learning Tcl syntax using its [official tutorial](https://www.tcl.tk/man/tcl8.5/tutorial/Tcl0.html)."
date = 2019-08-21T00:00:00-04:00
lastmod = 2021-05-19T16:44:20-04:00
tags = ["tcl"]
categories = ["unix"]
draft = false
creator = "Emacs 28.0.50 (Org mode 9.4.5 + ox-hugo)"
+++
Table of Contents
- [`tclsh` version](#tclsh-version)
- [Simple Text Output](#simple-text-output)
- [Assigning values to variables](#assigning-values-to-variables)
- [Evaluation & Substitutions 1: Grouping arguments with `""`](#evaluation-and-substitutions-1-grouping-arguments-with)
- [Evaluation & Substitutions 2: Grouping arguments with `{}`](#evaluation-and-substitutions-2-grouping-arguments-with)
- [Evaluation & Substitutions 3: Grouping arguments with `[]`](#evaluation-and-substitutions-3-grouping-arguments-with)
- [Results of a command - Math 101](#results-of-a-command-math-101)
- [
TODO Operands](#operands)
- [Math Functions](#math-functions)
- [Type Conversions](#type-conversions)
- [Computers and Numbers](#computers-and-numbers)
- [Numeric Comparisons 101 - `if`](#numeric-comparisons-101-if)
- [
TODO Textual Comparison - `switch`](#textual-comparison-switch)
- [Looping 101 - `while` loop](#looping-101-while-loop)
- [Looping 102 - `for` and `incr`](#looping-102-for-and-incr)
- [Adding new commands to Tcl - `proc`](#adding-new-commands-to-tcl-proc)
- [Variations in `proc` arguments and return values](#variations-in-proc-arguments-and-return-values)
- [
TODO Variable scope - `global` and `upvar`](#variable-scope-global-and-upvar)
- [Tcl Data Structures 101 - The list](#tcl-data-structures-101-the-list)
- [
TODO Adding & Deleting members of a list](#adding-and-deleting-members-of-a-list)
- [
TODO More list commands - lsearch, lsort, lrange](#more-list-commands-lsearch-lsort-lrange)
- [
TODO Simple pattern matching - "globbing"](#simple-pattern-matching-globbing)
- [
TODO String Subcommands - length index range](#string-subcommands-length-index-range)
- [
TODO String comparisons - compare match first last wordend](#string-comparisons-compare-match-first-last-wordend)
- [
TODO Modifying Strings - tolower, toupper, trim, format](#modifying-strings-tolower-toupper-trim-format)
- [Regular Expressions 101](#regular-expressions-101)
- [
TODO More Examples Of Regular Expressions](#more-examples-of-regular-expressions)
- [String replacement using regular expressions](#string-replacement-using-regular-expressions)
- [Removing square brackets in strings](#removing-square-brackets-in-strings)
- [
TODO More Quoting Hell - Regular Expressions 102](#more-quoting-hell-regular-expressions-102)
- [
TODO Associative Arrays](#associative-arrays)
- [
TODO More On Arrays - Iterating and use in procedures](#more-on-arrays-iterating-and-use-in-procedures)
- [
TODO Dictionaries](#dictionaries)
- [
TODO File Access 101](#file-access-101)
- [
TODO Information about Files - file, glob](#information-about-files-file-glob)
- [
TODO Invoking Subprocesses from Tcl - exec, open](#invoking-subprocesses-from-tcl-exec-open)
- [
TODO Learning the existence of commands and variables ? - info](#learning-the-existence-of-commands-and-variables-info)
- [
TODO State of the interpreter - info](#state-of-the-interpreter-info)
- [
TODO Information about procs - info](#information-about-procs-info)
- [
TODO Modularization - source](#modularization-source)
- [
TODO Building reusable libraries - packages and namespaces](#building-reusable-libraries-packages-and-namespaces)
- [
TODO Creating Commands - eval](#creating-commands-eval)
- [
TODO More command construction - format, list](#more-command-construction-format-list)
- [
TODO Substitution without evaluation - format, subst](#substitution-without-evaluation-format-subst)
- [
TODO Changing Working Directory - cd, pwd](#changing-working-directory-cd-pwd)
- [
TODO Debugging & Errors - errorInfo errorCode catch error return](#debugging-and-errors-errorinfo-errorcode-catch-error-return)
- [
TODO More Debugging - trace](#more-debugging-trace)
- [Command line arguments and environment strings](#command-line-arguments-and-environment-strings)
- [
TODO Leftovers - time, unset](#leftovers-time-unset)
- [
TODO Channel I/O: socket, fileevent, vwait](#channel-i-o-socket-fileevent-vwait)
- [
TODO Time and Date - clock](#time-and-date-clock)
- [
TODO More channel I/O - fblocked & fconfigure](#more-channel-i-o-fblocked-and-fconfigure)
- [
TODO Child interpreters](#child-interpreters)
- [Miscellaneous](#miscellaneous)
- [Line continuation](#line-continuation)
- [Setting array value](#setting-array-value)
- [Cycling through all elements in an array](#cycling-through-all-elements-in-an-array)
- [Cycling through all elements in a list](#cycling-through-all-elements-in-a-list)
- [List available namespaces](#list-available-namespaces)
- [List all procs in a namespace](#list-all-procs-in-a-namespace)
- [Reference](#reference)
## `tclsh` version {#tclsh-version}
```tcl
puts $tcl_version
```
```text
8.6
```
## Simple Text Output {#simple-text-output}
- If a string has more than one word (i.e. has space), it must be
enclosed in `" "` or `{ }`.
```tcl
puts "Hello, World - In Double Quotes"
puts {Hello, World - In Braces}
```
Note that single quotes have no significance in Tcl.
```text
Hello, World - In Double Quotes
Hello, World - In Braces
```
- If the string has no space, the quotes or braces are not needed.
```tcl
puts Hello
```
```text
Hello
```
- This is how you normally type a comment
```tcl
# This is a comment at beginning of a line
```
- A Tcl command is terminated by a newline or a semicolon.
```tcl
puts "This is line 1"
puts "this is line 2"
puts "This is line 3"; puts "this is line 4"
puts "Hello, World; - With a semicolon inside the quotes"
```
```text
This is line 1
this is line 2
This is line 3
this is line 4
Hello, World; - With a semicolon inside the quotes
```
- The same "semicolon ends a command" applies when ending a command
and starting a comment on the same line.
```tcl
puts "Hello, World - In quotes" ;# This is a comment after the command.
```
```text
Hello, World - In quotes
```
- Below will not work as there is no semicolon separating the command
and the comment.
```tcl
puts {Bad comment syntax example} # *Error* - there is no semicolon!
```
## Assigning values to variables {#assigning-values-to-variables}
`set` assigns a value to a variable and then also returns the same.
```tcl
set fruit Cauliflower
```
```text
Cauliflower
```
```tcl
set X "This is a string"
set Y 1.24
puts $X
puts $Y
puts "..............................."
set label "The value in Y is: "
puts "$label $Y"
```
```text
This is a string
1.24
...............................
The value in Y is: 1.24
```
The dollar sign tells Tcl to use the value of the variable - in this
case `X` or `Y`.
## Evaluation & Substitutions 1: Grouping arguments with `""` {#evaluation-and-substitutions-1-grouping-arguments-with}
Grouping words within double quotes allows substitutions to occur
within the quotations - or, in fancier terms, "interpolation". The
substituted group is then evaluated as a single argument.
In general, the backslash (`\`) disables substitution for the single
character immediately following the backslash. Any character
immediately following the backslash will stand without substitution.
However, there are specific "Backslash Sequence" strings which are
replaced by specific values during the substitution phase.
```tcl
puts "abc\n\tdef \u0A95"
```
```text
abc
def ક
```
```tcl
set Z Albany
set Z_LABEL "The Capital of New York is: "
puts "$Z_LABEL $Z" ;# Prints the value of Z
puts "$Z_LABEL \$Z" ;# Prints a literal $Z instead of the value of Z
puts "\nBen Franklin is on the \$100.00 bill"
set a 100.00
puts "Washington is not on the $a bill" ;# This is not what you want
puts "Lincoln is not on the $$a bill" ;# This is OK
puts "Hamilton is not on the \$a bill" ;# This is not what you want
puts "Ben Franklin is on the \$$a bill" ;# But, this is OK
puts "\n................. examples of escape strings"
puts "Tab\tTab\tTab"
puts "This string prints out \non two lines"
puts "This string comes out\
on a single line"
```
```text
The Capital of New York is: Albany
The Capital of New York is: $Z
Ben Franklin is on the $100.00 bill
Washington is not on the 100.00 bill
Lincoln is not on the $100.00 bill
Hamilton is not on the $a bill
Ben Franklin is on the $100.00 bill
................. examples of escape strings
Tab Tab Tab
This string prints out
on two lines
This string comes out on a single line
```
## Evaluation & Substitutions 2: Grouping arguments with `{}` {#evaluation-and-substitutions-2-grouping-arguments-with}
In contrast to words grouped in double quotes, no substitution happens
in words grouped in curly braces.
```tcl
set Z Albany
set Z_LABEL "The Capital of New York is: "
puts "\n................. examples of differences between \" and \{"
puts "grouped in double quotes: $Z_LABEL $Z"
puts {grouped in braces: $Z_LABEL $Z}
puts "\n....... examples of differences in nesting \{ and \" "
puts "braces in double quotes: $Z_LABEL {$Z}"
puts {double quotes in braces: Who said, "What this country needs is a good $0.05 cigar!"?}
puts "\n................. examples of escape strings"
puts {There are no substitutions done within braces \n \r \x0a \f \v}
puts {But, the escaped newline at the end of a\
string is still evaluated as a space}
```
```text
................. examples of differences between " and {
grouped in double quotes: The Capital of New York is: Albany
grouped in braces: $Z_LABEL $Z
....... examples of differences in nesting { and "
braces in double quotes: The Capital of New York is: {Albany}
double quotes in braces: Who said, "What this country needs is a good $0.05 cigar!"?
................. examples of escape strings
There are no substitutions done within braces \n \r \x0a \f \v
But, the escaped newline at the end of a string is still evaluated as a space
```
## Evaluation & Substitutions 3: Grouping arguments with `[]` {#evaluation-and-substitutions-3-grouping-arguments-with}
You obtain the results of a command by placing the command in square
brackets (`[]`). This is the functional equivalent of the back single
quote (`` ` ``) in shell scripting.
As the Tcl interpreter reads in a line it replaces all the $variables
with their values. If a portion of the string is grouped with square
brackets, then the string within the square brackets is evaluated as a
command by the interpreter, and the result of the command replaces the
square bracketed string.
Except ..
- A square bracket that is escaped with a `\` is considered as a
literal square bracket.
- A square bracket within braces is not modified during the
substitution phase.
```tcl
set x abc
puts "A simple substitution: $x\n"
set y [set x "def"]
puts "Remember that set returns the new value of the variable: X: $x Y: $y\n"
set z {[set x "This is a string within quotes within square brackets withing braces"]}
puts "Note that the curly braces prevented evaluation of the string in square brackets: $z\n"
set a "[set x {This is a string within braces within square brackets within double quotes}]"
puts "See how the set is executed: $a"
puts "\$x is: $x\n"
set b "\[set y {This is a string within braces beginning with an escaped square bracket within quotes}]"
# Note the \ escapes the bracket, and must be doubled to be a
# literal character in double quotes
puts "Note the \\ escapes the bracket:\n \$b is: $b"
puts "\$y is still \"$y\" from the first assignment"
```
```text
A simple substitution: abc
Remember that set returns the new value of the variable: X: def Y: def
Note that the curly braces prevented evaluation of the string in square brackets: [set x "This is a string within quotes within square brackets withing braces"]
See how the set is executed: This is a string within braces within square brackets within double quotes
$x is: This is a string within braces within square brackets within double quotes
Note the \ escapes the bracket:
$b is: [set y {This is a string within braces beginning with an escaped square bracket within quotes}]
$y is still "def" from the first assignment
```
## Results of a command - Math 101 {#results-of-a-command-math-101}
The Tcl command for doing math type operations is `expr`.