tcl_basic

Posted godlovepeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tcl_basic相关的知识,希望对你有一定的参考价值。

#*******************variable**************************
set NAME liugaopeng
set var 0
#set_app_var target_library [list *.db]

#*******************Processs Control******************
#if...elseif...else...
if {$var} {
echo "Good!"
} else {
echo "Bad!"
}

#switch
switch -regexp -exact $NAME {
"liugaopeng" {
echo "$NAME come to my office."
}
"zhaojianzhong" {
echo "Good morning!"
}
default {
echo "Error! The option is not exist."
}
}

#*****************Cycle***********************
#while
set i 0

while {$i < 10} {
echo "Current value of i is $i."
incr i 1;#i++
}

#for
for {set idx 0} {$idx < 10} {incr idx 1} {
if {$idx == 8} {
continue
}
echo "Current value of idx is $idx."
}

set NAMES [list "liugaopeng" "chen nan" "zhaojianzhong" "lizhi"]

#foreach
foreach NAME $NAMES {
echo "$NAME is here!"
}

set log_file_cnt 0
set txt_file_cnt 0

set file_list [glob *.log *.txt]

#example
foreach f_name $file_list {
set f_ext [file extension $f_name]
switch $f_ext {
".log" {incr log_file_cnt}
".txt" {incr txt_file_cnt}
}
}

echo "The total number of .log file is $log_file_cnt"
echo "The total number of .txt file is $txt_file_cnt"

#*****************File Operation*************************************
#Write info to it
set SRC "Good night"
set file_wr_id [open data.txt w+]
puts $file_wr_id $SRC
flush $file_wr_id
close $file_wr_id

#Read from a file
set DST ""
set file_rd_id [open data.txt r]
gets $file_rd_id DST
echo "Read from file is {$DST}."
close $file_rd_id

#****************Subroutine****************************************
#define subroutine
set data 10

proc print_var {} { ;#void input
global data
echo "data is $data";
}

#Invoking subroutine
print_var

proc max {a b} {
if {$a > $b} {
set y $a
} else {
set y $b
}
return $y;
}

proc sum {args} {
set num_list $args
set sum 0
foreach num $num_list {
set sum [expr ($sum + $num)]
}
return $sum
}

sum 1 8 9

array set AGES [list Liugaopeng 26 Chennan 25 Zhaojianzhong 36 lizhi 31]

proc inc_age {name_age_list} {
#convert list to array
array set OLD_AGES $name_age_list
set name_list [array names OLD_AGES]
foreach name $name_list {
incr OLD_AGES($name) 2
}
#convert array to list and output
return [array get OLD_AGES]
}

inc_age [list liu 25 chen 24]

set num 0

proc inc_one {num} {
upvar $num local_var
incr local_var 2
}























































































以上是关于tcl_basic的主要内容,如果未能解决你的问题,请参考以下文章