set list1 {hello\ =\ -1 world\ =\ -2 nihao\ =-3}
set list1 [lsort $list1]
puts [lindex $list1 1]
puts [lindex $list1 [expr [llength $list1] -1] ]
puts \n
set sum 0
foreach i $list1 {
regexp {^\w+\s*.*s*(-\d+)} $i total num
set sum [expr $sum + $num]
}
puts $sum
set string {a_b_c_f_d_g_h}
set list $string
puts $list
set list [split $list _]
set list [lsort $list]
puts $list
set flag 0
set a 0
set length [llength $list]
puts $length
set x 0
foreach i $list {
if {$flag==0} {
set a $i
set flag 1
} else {
set a $a\_$i
set flag 0
puts $a
}
set x [expr $x + 1]
if { $x == $length } {
puts $a
}
}
打印:
nihao =-3
world = -2
-6
a_b_c_f_d_g_h
a b c d f g h
7
a_b
c_d
f_g
h
tcl脚本
1. list的使用
2.正则匹配
3. 变量置换