Tcl代表Tool Command Language。tcl是一种基于字符串的命令语言,这种语言只有几种基础结构和相对较少的语法,非常易于学习。
1 2 3 4
|
#! /bin/tclsh # this is comment set var "hello world" puts $var
|
1 2 3
|
set s hello puts "the length of $s is [string length $s]" puts {the length of $s is [string length $s]}
|
1 2 3
|
set name "hello" string length $name => 5
|
1 2 3 4 5 6
|
if {[string compare $s1 $s2] == 0} { # strings are equal } if {[string equal $s1 $s2]} { # strings are equal }
|
1 2 3 4 5 6 7 8 9
|
string match a* alpha => 1
string match ?? xy => 1
set pat {[ab]*x} string match $pat box => 1
|
1 2 3 4
|
lappend new 1 2 => 1 2 lappend new 3 "4 5" => 1 2 3 {4 5}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
llength {a b {c d} "e f g" h} => 5 llength { } => 0
set x {1 2 3} lindex $x 1 => 2
# the last item lindex $list [expr {[llength $list] -2}] lindex $list end-1
# range lrange {1 2 3 {4 5}} 2 end => 3 {4 5}
|
1 2 3 4 5 6 7 8
|
linsert {1 2} 0 new stuff => new stuff 1 2 set x [list a {b c} e d] => a {b c} e d lreplace $x 1 2 B C => a B C d lreplace $x 0 0 => {b c} e d
|
1 2 3 4 5 6 7
|
if {$key < 0} { incr range 1 } elseif {$key == 0} { return $range } else { incr range -1 }
|
1 2 3 4
|
while {$i<$x} { set j [expr $j+$i] incr i }
|
1 2 3
|
for {set i 0} {$i<$x} {incr i} { set j [expr $j+$i] }
|
1 2 3
|
foreach value {1 3 2 11 5 4 7 6 9} { puts $value }
|
点击下载
]