Kyle Chen's Blog

Action speaks louder than Words

0%

Basic Commands in Tcl(Tool Command Language)

Introduction

What is Tcl(Tool Command Language)

Basic Commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/tclsh

# variable assign

set {a} 10
set b 12
set c 14
set d "14"
set e [expr $d + 10 + 9]
set res1 [expr $e / 9]; # the result is interger,Notice the semicolon here
set res2 [expr $e/ 9.0]; # the result is float,Notice the semicolon here

# variable print
puts $a
puts ${a}
puts $b
puts ${b}
puts " $b "
puts [expr $c + 10 + 9 ]
puts [expr $d + 10 + 9 ]
puts $e
puts $res1
puts $res2

#===========================================
# Variable Type -String
#===========================================

##the default type is string
set s1 hello
puts $s1


##use quotation marks
set s2 "hello world"
puts $s2


##use brace marks
set s3 {hello world}
puts $s3


##escape charater
set s4 "\\ \' \" \? \a \b \f \n \r \t \v "
puts $s4


##string command

puts [string compare $s1 $s2]
puts [string compare $s2 "hello world"]
puts [string length $s1]
puts [string tolower $s1]
puts [string index $s1 2]
## refer to the TCL manual for more other string related commands
## index,length,range,tplower,toupper,trimleft .etc..





#===========================================
# Variable Type -List
#===========================================




set list1 {a b c}
set list2 [list a b c]
set list3 [split "a_b_c" _]
puts $list1
puts $list2
puts $list3

## index
puts [lindex $list3 2]


## append
append list1 " " "d"
append list1 "d"
lappend list1 "d"
puts $list1


## length
puts [llength $list1]


## index
puts [lindex $list1 1]

## insert
puts "insert"
set list1 [linsert $list1 3 hi asd asd]
puts $list1
##puts [linsert $list1 3 hi asd asd]

## replace
set list1 [lreplace $list1 1 1 hello ]
puts $list1

## set
#puts [lset list1 0 world]

## assign
#lassign list1 tmp_list1 tmp_list2
#puts $tmp_list1
#puts $tmp_list2

## sort
set list1 [lsort $list1]
puts $list1


#================================
# Variable Type - Array
#================================

set array1(0) chen
set array1(1) weijie
puts $array1(0)
puts $array1(1)
puts [array size array1]
puts "iterats"
for { set index 0 } { $index < [array size array1] } { incr index } {
puts "${index}"
puts "$array1($index)"
}


set person1(age) 80
set person1(name) chen
foreach index [array names person1] {
puts "$index"
puts "$person1($index)"
}




#=====================

#Variable Type - dict

#======================

##dict create d1 name chen age 17

##dict set d2 name Jason
##dict set d2 age 18

##set d3 [dict create name Bob age 19]

###puts $d1
##puts $d2
##puts $d3
##puts [dict size $d1]

### refer to document for more details





#=====================
# Condition Statement
#=====================


set a 30

if { $a == 10 } {

puts "Value is 10"

} elseif { $a == 20 } {

puts "Value is 20"

} else {

puts "no matching value"
}





switch $a {

10 {
puts "the value is 10"
}
20 {
puts "the value is 20"
}
default {
puts "no matching value"
}

}



set b [expr $a == 10 ? 10 : 20]
puts "the value of b is : $b \n"





#=====================

#Loop Stament

#=====================

while { $a < 40 } {
incr a
puts " the value of $a "
}


for { set index $b} { $index < 30 } { incr index } {

puts " the value of index : $index "

if { $index == 21 } {
incr index
continue
}

if { $index == 25 } {
break
}

}



#====================

#Proc

#====================

proc func1 {} {

puts "hello world"

}

proc func2 { a b } {
return [ expr $a + $b ]

}


proc func3 { numbers } {

set sum 0
foreach number $numbers {
set sum [ expr $sum + $number ]
}
set avg [ expr $sum / [ llength $numbers ]]
return $avg
}


proc func4 { a { b 10 } } {

return [ expr $a + $b ]
}



proc func5 { num } {
if { $num == 1 } {
return 1
}

return [ expr $num * [ func5 [ expr $num -1 ]]]


}



func1

puts [ func2 10 20 ]

puts [func3 {10 20 30}]

puts [func4 10 ]

puts [ func4 10 20 ]

puts [ func5 5 ]



#================

#NameSpace

#===================



## define namespace
namespace eval MyMath {

variable myResult

namespace eval MyMath_Inner {

variable myResult1

}

}

## set the variable And proc of the namespace
proc MyMath::Add { a b } {
set ::MyMath::myResult [ expr $a + $b ]
set ::MyMath::myResult1 [ expr $a - $b ]

}



## print the varible And proc output of the namespace
MyMath::Add 10 23
puts $::MyMath::myResult
puts $::MyMath::myResult1




## export the porc of the namespace

namespace eval testNamespace {

namespace export testProc
}

proc testNamespace::testProc {a b} {
return [expr $a * $b ]

}



namespace import testNamespace::*
puts [ testProc 10 20 ]
namespace forget testNamespace::*





#=======================
#File IO
#=======================

## write file

set fp [ open "FileIoTest" w+]
puts $fp "test"
close $fp


## read file
set fp [open "FileIoTest" r]
set content [read $fp]
puts $content
close $fp


# read file line by line

set fp [open "FileIoTest2" w+]
puts $fp "hello \n world"
close $fp


set fp [open "FileIoTest2" r]
while { [gets $fp data] >0 } {
puts $data
}
close $fp




#===================
#Error & Catch
#===================


proc Div {a b} {
if {$b == 0} {
error "Error generated by error" "Info String for error" 401
} else {
return [expr $a/$b]
}
}
if {[catch {puts "Result = [Div 10 0]"} errmsg]} {
puts "ErrorMsg: $errmsg"
puts "ErrorCode: $errorCode"
puts "ErrorInfo:\n$errorInfo\n"
}

if {[catch {puts "Result = [Div 10 2]"} errmsg]} {
puts "ErrorMsg: $errmsg"
puts "ErrorCode: $errorCode"
puts "ErrorInfo:\n$errorInfo\n"
}