\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{AHA} \pdfinfo{ /Title (tcl-language.pdf) /Creator (Cheatography) /Author (AHA) /Subject (Tcl Language Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{C4DE1D} \definecolor{LightBackground}{HTML}{FBFCF0} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Tcl Language Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{AHA} via \textcolor{DarkBackground}{\uline{cheatography.com/22239/cs/18129/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}AHA \\ \uline{cheatography.com/aha} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 18th September, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List operations}} \tn % Row 0 \SetRowColor{LightBackground} lappend {\bf{\textless{}list\textgreater{}}} \$ele & \$ele is added to list as a single index \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{set A1 {[}concat \$list1 "ABC" \$list2 \$var2{]}; {\emph{\#concat will add lists as a separate indexes, if list 1 contains 3 elements it will add 3 index values; ○ If you want to maintain list structure use lappend, if you want simple list with all the elements separate use concat}}} \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} llength \textless{}\$list\textgreater{} & length of a list \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{lindex \textless{}\$list\textgreater{} \textless{}\#index\_no\textgreater{} ?\textless{}\#2nd index\_no in case of nested list\textgreater{}?} \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{lrange \textless{}\$list\textgreater{} \textless{}\#1st\_index\textgreater{} \textless{}\#2nd\_index\textgreater{}; \#Returns elements from 1st index to 2nd index} \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{linsert \textless{}\$var\textgreater{} \textless{}\#index\_position\textgreater{} \textless{}ele1\textgreater{} \textless{}ele2\textgreater{} \textless{}ele3\textgreater{} .... ; {\emph{\#Output: will have list added at index position}}} \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{linsert \textless{}\$list\textgreater{} \textless{}\#index\_position\textgreater{} \textless{}ele1\textgreater{} \textless{}ele2\textgreater{} \textless{}ele3\textgreater{} .... ;}} {\emph{\# Output will have separate elements, first index will be WHOLE original list}}} \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{lreplace \textless{}\$list\textgreater{} \textless{}\#start\_index\textgreater{} \textless{}\#end\_index\textgreater{} \textless{}item1\textgreater{} \textless{}item2\textgreater{} \textless{}item3\textgreater{}...; {\emph{\# replace items between start and end indexes with item1, item2 .....}}} \tn % Row Count 22 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{lsearch ?-\textless{}all| dictionary| decreasing| glob| exact| regexp| not| sorted| integer| real| \#start\_index\textgreater{}? \textless{}\$list\textgreater{} \textless{}pattern\textgreater{}} \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{lsort ?-\textless{}ascii| dictionary| integer| real| increasing| decreasing| \#index\_sublist| unique\textgreater{}? \textless{}\$list\textgreater{} {\emph{\# index option =3 will sort the main list based on 4th element of each sublist}}} \tn % Row Count 29 (+ 4) % Row 10 \SetRowColor{LightBackground} split \textless{}\$list/\$var\textgreater{} ?\textless{}chars\textgreater{}? & {\bf{String to list}} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List operations (cont)}} \tn % Row 11 \SetRowColor{LightBackground} join \textless{}\$list/\$var\textgreater{} ?\textless{}chars\textgreater{}? & {\bf{List to string}} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{○ set random {[}list a b c \{bad\} \{ had \} 123 1a2b "1.22" 15.43 \$had \$tag \{A B C D E F G\}{]}; \#Here each element is a separate element, each list with grouping is a single element}} \newline {\emph{○ list \$a \$b \$c; \#Here list contains 3 elements only, even though a b or c contains list inside it}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{String operations}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{string match ?-nocase? \textless{}pattern\textgreater{} \textless{}string\textgreater{}}} {\emph{\# if pattern matches string output is 1}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{string equal ?-nocase|-length int? \textless{}string1\textgreater{} \textless{}string2\textgreater{}}} \# Returns 1 if string1 and string2 are identical, or 0 when not.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{string compare ?-nocase|-length \#N? \textless{}string1\textgreater{} \textless{}string2\textgreater{}}} \#check strings lexicographically, if string1\textgreater{} string2 output is 1} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{string map \{\textless{}from\textgreater{} \textless{}to\textgreater{}\} \textless{}string\textgreater{}}} \#string map \{H 000\} \$a 17 \#replace H in Hello with 000} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{string replace \textless{}string\textgreater{} \textless{}\#start\_index\textgreater{} \textless{}\#end\_index\textgreater{} ?\textless{}replacement\_string\textgreater{}?}}} \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{subst ?-nobackslashes? ?-nocommands? ?-novariables? string}} {\emph{\#set a 44 ;subst \{xyz \{\$a\}\}; output is xyz\{44\}}}} \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{string range \textless{}string\textgreater{} \textless{}\#start\_index\textgreater{} \textless{}\#end\_index\textgreater{}}}} \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{puts {[}format "Today is \%s \%d \%f" \$day \$month \$year{]}}}} \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string length \textless{}string\textgreater{}} \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string first \textless{}char or pattern to search\textgreater{} \textless{}string\textgreater{} ?\textless{}start index\textgreater{}?} \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string index \textless{}string\textgreater{} \textless{}index \#| end|end-n\textgreater{}} \tn % Row Count 23 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string bytelength \textless{}string\textgreater{}} \tn % Row Count 24 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string last \textless{}char or pattern to search\textgreater{} \textless{}string\textgreater{} ?\textless{}end index\textgreater{}?} \tn % Row Count 26 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string is \textless{}class\textgreater{} \textless{}string\textgreater{} \seqsplit{\#class==alpha|ascii|boolan|control|digit|double|false|graph|integer|list|lower|space|punct|true|upper|lower}} \tn % Row Count 29 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string repeat \textless{}string\textgreater{} \textless{}\#count\textgreater{}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{String operations (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string toupper/tolower/totitle \textless{}string\textgreater{} ?\textless{}\#start\_index\textgreater{}? ?\textless{}\#end\_index\textgreater{}?} \tn % Row Count 2 (+ 2) % Row 16 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string trimleft/trimright/trim \textless{}string\textgreater{} ?\textless{}chars\textgreater{}?} \tn % Row Count 3 (+ 1) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{append \textless{}string\_var\textgreater{} \$x, \$y \# "ZZZ"} \tn % Row Count 4 (+ 1) % Row 18 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string reverse \textless{}string\textgreater{}} \tn % Row Count 5 (+ 1) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{string wordend|wordstart \textless{}string\textgreater{} \textless{}\#index\textgreater{}} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`array set array1 {[}list \{123\} \{Abigail Aardvark\} \textbackslash{} \{234\} \{Bob Baboon\} \textbackslash{} \{345\} \{Cathy Coyote\} \textbackslash{} \{456\} \{Daniel Dog\} {]}`} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{set fruit(Apple) 143} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{array size \textless{}arrayname\textgreater{} ; \#} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{array names \textless{}arrayname\textgreater{} ?\textless{}pattern\textgreater{}?;\# gives all the keys as iteratable list} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{array get \textless{}arrayname\textgreater{}; \# returns list where each odd member is key and even is value} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{array exists \textless{}arrayname\textgreater{}} \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`foreach key {[}array names array1{]} \{ puts "Key is \$key and value is \$array1(\$key)"; \}`} \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{parray \textless{}arrayname\textgreater{}; \#} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 14 (+ 0) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{array startsearch \textless{}arrayName\textgreater{}; \#} \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{array nextelement \textless{}arrayName\textgreater{} \textless{}searchID\textgreater{} ;\#} \tn % Row Count 16 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{array anymore \textless{}arrayName\textgreater{} \textless{}searchID\textgreater{}} \tn % Row Count 17 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{array donesearch \textless{}arrayName\textgreater{} \textless{}searchID\textgreater{}} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{dict set \textless{}DICT NAME\textgreater{}\textless{}Key1\textgreater{} \textless{}value1\textgreater{}}} {\emph{\#create a dict with DICT\_NAME key and value}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{dict set \textless{}DICT NAME\textgreater{}\textless{}Key1\textgreater{} \textless{}nested Key1\_1\textgreater{} \textless{}value1\textgreater{}}} {\emph{\#create a dict with DICT\_NAME key and value}}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{set \textless{}DICT NAME\textgreater{} {[}dict create 1 "SK" 2 "KK" 3 "ZK"{]}}} {\emph{\#1 is key and SK in value}}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{dict unset names \textless{}\$DICT\_NAME\textgreater{}}} ; \#removes key/value pair} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict replace \textless{}\$DICT NAME\textgreater{} \textless{}key\textgreater{} \textless{}new\_value\textgreater{} ;\#replace the value corresponding to key} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{dict keys \textless{}\$DICT\_NAME\textgreater{} ; \#provides all the keys as list} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict values \textless{}\$DICT\_NAME\textgreater{}; \#provides all values as list} \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{dict get \$\textless{}DICT\_NAME\textgreater{} \textless{}key\textgreater{} ;\#returns value on that key:ZK} \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict get \$DICT\_NAME ;\#returns all key and value pair as a list} \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{`dict for \{key value\} \$DICT \{ puts "Key:\$key Value:\$value";\}`}}} \tn % Row Count 21 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{`foreach keys {[}dict keys \$DICT{]} \{ puts "Key:\$keys Values {[}dict get \$DICT \$keys{]}"; \}`}}} \tn % Row Count 23 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{dict append \textless{}DICT\_NAME\textgreater{} 4 LA;\#adds one key to dictionary} \tn % Row Count 25 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict lappend \textless{}DICT\_NAME\textgreater{} 4 LA SF PO;\#adds values as a single value to key 4} \tn % Row Count 27 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{set filtered {[}dict filter \$\textless{}DICT\_NAME\textgreater{} key|value|script 1{]} ;\#filter by name value or script} \tn % Row Count 29 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{set filtered {[}dict filter \$\textless{}DICT\_NAME\textgreater{} script \{key value\} \{Expr \{\$key \textless{} 3 \}\}{]} ;\# returns 1 SK 2 KK} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict exists \$names \textless{}key\textgreater{} ;\# Checks for key 3 in names dict, returns 1 is key exists} \tn % Row Count 2 (+ 2) % Row 16 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{dict incr \$names one\textless{}key\textgreater{} 4\textless{}increment by\textgreater{} ;\#increments value by integer \textless{}value\textgreater{}} \tn % Row Count 4 (+ 2) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict info \$names ; \# provides info on dict} \tn % Row Count 5 (+ 1) % Row 18 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{set merged {[}dict merge \$test1 \$test2{]} ; \#merges two dicts} \tn % Row Count 7 (+ 2) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{set new {[}dict remove \$test \textless{}key1\textgreater{} \textless{}key2\textgreater{} ....{]} ; \# removes key/value pair based on specif ied keys} \tn % Row Count 10 (+ 3) % Row 20 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\#\#\#\# Dictionary modification} \tn % Row Count 11 (+ 1) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict size \$names ; \#get the number of key/value pairs} \tn % Row Count 13 (+ 2) % Row 22 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`\# Define a simple dict: \{a b c d\} ; set dd {[}dict create a b c d{]} ; dict update dd a aVar c cVar \{ set aVar 2; set cVar {[}string toupper \$cVar{]}; \} \# displays \{a 2 c D\} set dd`} \tn % Row Count 17 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Dictionary Examples}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict for \{key value\} \$names \{ \newline puts "Key is: \$key and Value is: \$value" \newline \} \newline foreach key {[}dict keys \$name\_number\_dict{]} value {[}dict values \$name\_number\_dict{]} \{ \newline puts "\$key == \$value" \newline \} \newline \newline \#dict update \newline \% set didi \{key1 value1 key2 value2\} \newline key1 value1 key2 value2 \newline \% dict update didi key1 varKey1 key2 varKey2 \{ \newline append varKey1 new \newline append varKey2 new \newline unset varKey1; \#deletes key value pair \newline \} \newline value2new \newline \% set didi \newline key1 value1new key2 value2new \newline \#dict with : you can convert key names to variables directly \newline \% set pers\_detail {[}dict create forenames Joe surname Schmoe street \{147 Short Street\} \textbackslash{} \newline city Springfield phone 555-1234{]} \newline dict with pers\_detail \{ \newline puts " Name: \$forenames \$surname" \newline puts " Address: \$street, \$city" \newline puts " Telephone: \$phone" \newline \} \newline \newline \newline {\bf{\#\#Inventory system \newline KEY FIRST LAST TITLE \newline 1 Clif Flynt Tcl/Tk For Real Programmers \newline 2 Clif Flynt Tcl/Tk: A Developer's Guide 2'nd edition \newline 3 Brent Welch Practical Programming in Tcl/Tk \newline 4 Michael McClennan Effective Tcl/Tk Programming \newline 5 Don Libes Exploring Expect \newline \newline dict set books 1 {[}list first Cliff last Flint title "Tcl/TK a dev guide" year 2009{]} \newline \newline dict set books 2 first Brent \newline dict set books 2 last Welch \newline dict set books 2 title "Practical Programming in Tcl/Tk" \newline dict set books 2 year 1867 \newline \newline foreach \{sr first last title year\} \{ \newline 3 Michael McClennan \{Effective Tcl/Tk Programming\} 2001 \newline 4 Don Libes \{Exploring Expect\} 2008 \} \{ \newline dict set books \$sr {[}list first \$first last \$last title \$title year \$year{]} \newline \} \newline \#set first firsttest \newline dict for \{sr infor\} \$books \{ \newline dict with infor \{ \newline puts "\$sr \$first \$last \$title \$year" \newline \} \newline \}}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Looping}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{while \{\textless{}test exp\textgreater{}\} \{ \textless{}body\textgreater{} \}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{for \{set i 10\} \{\$i\textgreater{}=0\} \{incr i-1\} \{ \textless{}body\textgreater{} \}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{foreach ele \$list \{ \textless{}body\textgreater{} \}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{foreach \{ele1 ele2\} \$list \{ \textless{}body\textgreater{} \}; \# 2 elements are taken from list2 for each iteration} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{foreach ele1 \$list1 ele2 \$list2 \{ \textless{}body\textgreater{} \}; \# l1 iterates over list1 and l2 iterates over list2} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Switch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{• switch ?-option (exact, glob, regexp, nocase) -{}-? \$string \textbackslash{} \newline \$pattern\_1 \{body\} \textbackslash{} \newline \$pattern\_2 \{body\} \textbackslash{} \newline . \newline . \newline default \{body\}; \newline \newline • switch ?-option (exact, glob, regexp, nocase) -{}-? \$string \textbackslash{} \newline \{A body\} \newline \{B body\} \newline . \newline . \newline \{default body\}; \newline Here pattern substitutions cannot occur} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{procs}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{• Proc with optional arguments \newline proc random\_num \{min \{max 100\}\} \{ \newline \textless{}body\textgreater{} \newline \} \newline • Proc with variable number of arguments \newline proc random\_num \{args\} \{ \newline puts \$args; llength \$args; lindex \$args 0; \newline \textless{}body\textgreater{} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{regex}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{regexp ?}} {\emph{\textless{}about| expanded| indices| line| linestop| lineanchor| nocase| all| inline| start| \#index\_start\textgreater{}?}} {\bf{\{\textless{}regex\_to\_match\textgreater{}\} \textless{}\$string\textgreater{} match submatch1 submatch2 .....}}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{regsub ?}} {\emph{\textless{}all| expanded| line| linestop| lineanchor| nocase| inline| \#index\_start\textgreater{}?}} {\bf{\{\textless{}regex\_to\_match\textgreater{}\} \textless{}\$string\textgreater{} \{\textless{}regex\_to\_replace\textgreater{}\}}} {\emph{?\textless{}storage\_variable\textgreater{}?}}} \tn % Row Count 8 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Regsub \newline ○ \textbackslash{}1: 1st grouping match \newline ○ \textbackslash{}2: 2nd grouping match \newline ○ \&: whole expression/inside \{\} match \newline ○ command returns replaced string dictated by regex to replace \{\} \newline ○ if storage\_variable specified, return 1 or 0 depending on \textless{}regex to match\textgreater{} or not \newline ○ `regsub \{(\textbackslash{}w+).(\textbackslash{}w+)\} "report.txt" \{\textbackslash{}1.tcl\} ext` \newline `puts \$ext (output will be report.tcl)`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{File IO}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{set FILEHANDLE {[} open \textless{}filename\textgreater{} ?access\_mode (r| r+| w| w+| a | a+)? ?permissions(755, 744, 777)?} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{close \$FILEHANDLE} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{if \{{[}catch \{set FILEHANDLE {[} open \textless{}filename\textgreater{} r+{]} \} ?\textless{}error\_var\textgreater{}?{]}\} \{ puts \$error\_var; \textless{}body\textgreater{} \}} \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{r+ mode does not create a file if it does not exists, where a+/w+ will create a file if it does not exist}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Reading a file proc (memory)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{proc read\_file\_less\_memory \{file\_in\} \{ \newline if \{{[}file exists \$file\_in{]}\} \{ \newline set fh {[}open \$file\_in r{]}; \newline {\bf{while \{{[}gets \$fh line{]} \textgreater{}= 0\} \{}} \newline {\bf{ lappend file\_data \$line;}} \newline \} \newline close \$fh; \newline {\bf{return \$file\_data;}} \newline \} else \{ \newline puts "File does not exist." \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Reading a file proc}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{proc read\_file \{f\} \{ \newline puts "Reading :\textbackslash{}t \$f"; \newline {\bf{if \{{[}catch \{set fh {[}open \$f r{]}\} err{]} \} \{}} \newline puts "File does not exist \$f:\$err"; \newline \} else \{ \newline set fh {[}open \$f r{]}; \newline {\bf{set fdata {[}split {[}read \$fh{]} "\textbackslash{}n"{]}}} \newline close \$fh; \newline return \$fdata; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.96 cm} x{5.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{MISC}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{User input}} & {\bf{gets stdin \textless{}VAR NAME TO ASSIGN VALUE\textgreater{}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Random number & set i {[}expr \{int(rand() * 6)\}{]} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} global & change the scope of the variables to global from inside proc \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Upvar & Used to pass by value to a proc, while instantiating proc do not provide values with a \$sign, leave out \$sign ; ` \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} argc & number of command line arguments \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} argv0 & Script name \tn % Row Count 15 (+ 1) % Row 6 \SetRowColor{LightBackground} argv & all command line arguments \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} set A1 {[}lindex \$argv 0{]} & first argument \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Error Handling}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Error Handling:}} \newline \newline proc Div \{a b\} \{ \newline if \{\$b == 0\} \{ \newline {\bf{error "Error generated by error" "Info String for error" 401}} \newline {\bf{ error \textless{}Error Message\textgreater{} \textless{}Error Info\textgreater{} \textless{}Error code\textgreater{}}} \newline \} else \{ \newline return {[}expr \$a/\$b{]} \newline \} \newline \} \newline \newline if \{{[}catch \{puts "Result = {[}Div 10 0{]}"\} errmsg{]}\} \{ \newline {\bf{puts "ErrorMsg: \$errmsg"}} \newline {\bf{puts "ErrorCode: \$errorCode"}} \newline {\bf{puts "ErrorInfo:\textbackslash{}n\$errorInfo\textbackslash{}n"}} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Upper/Global}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Global/By value:}} \newline \newline proc by\_global \{A B C\} \{ \newline global j1; \newline global j2; \newline global j3; \newline \newline set j1 \$A; set j2 \$B; set j3 \$C; \newline puts "In \$j1 \$j2 \$j3" \newline \} \newline \newline by\_global 100 200 300 \newline \newline {\bf{Upvar/By reference:}} \newline proc by\_reference \{P Q R\} \{ \newline \newline upvar \$P p; \newline upvar \$Q q; \newline upvar \$R r; \newline set p {[}expr \$p{\emph{2{]}; \newline set q {[}expr \$q}}2{]}; \newline set r {[}expr \$r*2{]}; \newline \newline puts "Inside upvar: \$p \$q \$r"; \newline \newline \} \newline \newline \newline {\bf{by\_reference j1 j2 j3;}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{To Do}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Multi- Dimensional Arrays} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{How to set command line arguments for a scripts?} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{packages vs module vs namespace vs proc}}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{namespace} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{parse\_proc\_arguments} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{seek/tell} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{upvar} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{uplevel} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{lset} \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{string map} \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{How to set up Help for a script?} \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\$argv and \$argc} \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{global} \tn % Row Count 13 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Scan} \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Current Time \& Date}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{set systemTime {[}clock seconds{]} \newline set current\_time {[}clock format \$systemTime -format \%H:\%M:\%S{]} \newline set ct {[}clock format \$systemTime -format \%H\_\%M{]} \newline set current\_date {[}clock format \$systemTime -format \%D{]} \newline set cdt {[}clock format \$systemTime -format \%m\_\%d\_\%y{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}