===================
== lxulxu's blog ==
===================
Hello there

Tcl Cheat Sheet

cheat-sheet tcl
  • 基础语法
 1puts "Hello World"#输出
 2set x [expr 1 == 2 ? 1 : 2]#设置变量;三元运算符
 3[exec ls]#调用子进程
 4
 5#if
 6if { $x < 2} { ... }
 7elseif { $x == 1 } { ... }
 8
 9#while
10while { $x < 2 } {
11	incr x
12}
13
14#for
15for { set i 0 } { $i < 5 } { incr i } { ... }
16
17#switch
18switch $x {
19	1 { ... }
20	2 { ... }
21	default { ... }
22}
  • 字符串
 1set s1 "_Hello_World_"
 2set s2 "_"
 3
 4[string length $s1]
 5[string index $s1 0]
 6[string range $s1 1 end]
 7append s1 "!"
 8[string repeat $s1 5]#"_Hello_World_"*5
 9
10[string toupper $s1]
11[string tolower $s1]
12
13[string compare $s1 $s2]
14[string equal $s1 $s2]
15
16[string first $s2 $s1]#s1中第一个匹配s2索引
17[string last $s2 $s1]#s1中最后一次匹配s2索引
18
19[string trim $s1 $s2]#->Hello_World
20[string trimright $s1 $s2]#->_Hello_World
21[string trimleft $s1 $s2]#->Hello_World_
22
23set s1 "abc@163.com"
24set s2 "*@*.com"
25[string match $s2 $s1]
26
27[format "%s %x %f" "tcl" 40 43.5]
28scan {planet1 0.330 planet2 4.87} {planet1 %g planet2 %g}#-> 0.330 4.87 
  • 列表
 1#初始化
 2set colors {red green blue}
 3set colors [split "red_green_blue" _]
 4set colors [list red green blue]
 5
 6[lindex $colors 1]
 7[lrange $colors 0 2]
 8[llength $colors]
 9lappend colors "orange"#末尾添加元素
10lset colors 3 purple#设置第n个元素
11[linsert $colors 3 black white]
12[lreplace $colors 0 1 black white]
13[lsearch $colors black]
14concat $colors {black {white purple}}#->$colors black {white purple}
15[join $colors /]#列表元素以特定分隔符组合为字符串
16[lsort $colors]
17foreach item $colors { ... }#遍历
  • 字典
 1#初始化
 2dict set colors c1 red
 3set colors [dict create c1 "black" c2 "white"]
 4
 5[dict size $colors]
 6[dict keys $colors]#所有key组合成的列表
 7[dict values $colors]#所有value组合成的列表
 8[dict exists $colors c1]
 9#遍历
10foreach item [dict keys $colors] {
11	set value [dict get $colors item]
12}
13dict for {key value} $colors {
14	puts "$key : $value"
15}
  • IO
 1#写入文件
 2set fp [open "input.txt" w+]
 3puts $fp "Hello world."
 4close $fp
 5#读取文件
 6set fp [open "input.txt" r]
 7set file_data [read $fp]
 8close $fp
 9
10set fp [open "input.txt" r]
11while {% [gets $fp data] >= 0 %} {%
12  puts $data
13%}
14close $fp
15#文件名相关操作
16[file join C:/ {Program Files} Tcl tclsh.exe]#->C:/Program Files/Tcl/tclsh.exe
17[file split x/y/z]#->x y z
18[file dirname /a/b/main.c]#->/a/b
19[file extension src/main.c]#->.c
20[file rootname src/main.c]#->src/main
21[file tail /a/b/c.txt]#->c.txt
22#查找文件
23glob -nocomplain {{src,backup}/*.[ch]}]#-nocomplain:跳过搜索结果为空时报错;{{src,backup}/*.[ch]}:正则匹配搜索文件
24[glob -directory ./ -types f -tails *]#-directory:指定搜索文件夹;-types:指定搜索对象类型
25#文件相关操作
26file mkdir foo
27file delete a.txt b.txt *.tmp
28file delete {*}[glob *]
29file copy a.txt b.txt#a.txt复制到b.txt
30file rename b.txt c.txt#b.txt重命名为c.txt
  • 正则匹配
1#匹配
2regexp {([a-z]*).([a-z]*)} "Tcl Command" sm sm1 sm2#sm:full match;sm1:sub match 1;sm2:sub match 2
3regexp -nocase -start 4 -line -- {([a-z]*).([a-z]*)} "Tcl\nCommand" sm sm1 sm2#-nocase:忽视大小写;-start:指定匹配开始位置;-line:跨行匹配
4#替换
5regsub there "They lives there lives" their s#->s:They lives their lives
6regsub -all a ababa zz s#->s:zzbzzbzz
7regsub -all -- (a+)(ba*) aabaabxab {z\2} s#->s:zbaabxzb
  • 数学运算
 1namespace import ::tcl::mathfunc::*
 2
 3[rand]
 4set x -1.2
 5set y 2
 6[abs $x]
 7[ceil $x]#向上取整
 8[floor $x]#向下取整
 9[fmod $x $y]#%
10[int $x]
11[pow $x $y]
12[round $x]
13[sqrt $y]
14[max $x $y]
15[min $x $y]
  • 时间
1set curr_tm [clock seconds]
2[clock format $curr_tm -format %H:%M:%S]#->15:34:01
3[clock format $curr_tm -format %D]#08/29/2022
4
5set date "Jun 15, 2014"
6[clock scan $date -format {%b %d, %Y}]#->1402761600
7
8after 500#休眠n毫秒
  • 函数
 1proc add {a b} {
 2	return [expr $a + $b]
 3}
 4
 5[add 10 30]
 6
 7#错误与异常
 8catch {set fp [open no.txt]} errmsg
 9puts "ErrorMsg: $errmsg"
10
11proc div {a b} {
12  if {$b == 0} {
13    error "Error generated by error" "Info String for error" 401
14  } else {
15    return [expr $a / $b]
16  }
17}
18
19if {[catch {puts "Result = [div 10 0]"} errmsg]} {
20  puts "ErrorMsg: $errmsg"
21  puts "ErrorCode: $errorCode"
22  puts "ErrorInfo:\n$errorInfo\n"
23}