Anda di halaman 1dari 9

Tcl

Tcl("Tool Command Language",Tcl"tickle" )


,
TclTclTcl
TclC/C++Tcl
TK,TK OS GUI(Python
GUITK)Expect
passwd, ftp, telnet

Tcl
1
TclshellTcl
(field)TAB

TclTcl
(substitution)Tcl
C/C++

TclTclC/C++,
Tcl_CreateCommand

Tcl' 'Tcl

set a 100

# Not a comment

set b 101; # this is a comment


'#'set
'#'

2Substitution
set x 10
set y 100 + x
y100 + x110Tcl
x 100 + xx '10'

Tclx 'x'Tcl
Tcl
Tcl

1variable substitution

set x 10
set y 100 + $x
y11010+100Tcl10+100
y110Tcl10+100
2command substitution
[]Tcl

set x 10
set y [expr 100+$x]
y110Tcl'['expr
exprC/C++expr'10+110'

[]Tcl[]
3backslash subtitution)
TclC
[$Tcl
set msg multiple\ space
'\'Tcl
set '\''multiple space'
(word)
4
Tcl
({})
Tcl[]
Tcl

Tcl
Tclstring

,
1

Tcl
C\C++Tcl

:
set a 2
set a.1 5
set b $a.1
a.1bTcl$
('.')('a')
Tcla2,2.1b

set b ${a.1}
procedureset procedureprocedure
procedureglobal
append -

append varName ?value value value ...?

varNamevalue.varName,value.
varName..$a
,append a $b set a $a$b .

.
set var 0
for {set i 1} {$i<=10} {incr i} {
append var "," $i
}
puts $var
# Prints 0,1,2,3,4,5,6,7,8,9,10
2
TclTcl

Tcl
set day(monday) 1
set day(sunday) 2

3string
TCLTCL
format
format formatstring vlue value...
format ANSICsprintfformatstringvalue
forma-tstring
set msg [format "%s is %d years old" $name $age]
scan
scan string formatsting varName varName ...
scanformat ANSI Csscanfformatsting
stringvarName,TABstring
formatsting'%'
scan "some 26 34" "some %d %d" a b
regexp
regexp [switchs] [--] exp string [matchVar]\ [subMatchVar subMatchVar...]
regexpexpstring10
regexpswitchs -nocase-line--
switchs '-'
regexpmatchVarsubMatchVar

regexp

regexp { ([0-9]+) *([a-z]+)} " there is 100 apples" total num word
puts " $total ,$num,$word"
100 apples ,100,apples
regsub
regsub [switchs] exp string subSpec varname
regsubregexp
10regsubstring

regsub there "They live there lives " their x


puts $x
They live their lives
sting
string subcmd arg [arg...]
stringsubcmd

string length string


string range string first last

string index string charIndex


string tolower string [first] [last]
string replace string first last [newstring]
string equal [-nocase] [-length int] string1 string2
string match [-nocase] pattern string pattern string,1,0.
string compare [-nocase] [-length int] string1 string2 -length int

string first string1 string2 [startindex] string2 string1


(0-based)-1
string trim string [chars] :stringchars
chars spaces tabs newlines carriage returns .
4list
list TCLTCLlist list
list list Tcllist
list
list [ value value...]
list list value
concat
concat list [list...]
list list
llength
llength list
list
lindex
lindex list index
list index (0-based)
lrange
:lrange list first last
list first (0-based)last (0-based),last endfirst

linsert
linsert list index value [value...?]
valuelist index (0-based)
lappend
lappend varname value [value...?]
valuevarnamevarname

lreplace
lreplace list first last [value value ...]

list firs (0-based)t last (0-based)value


valuefirst last
lsort
lsort [options?] list
list options
-ascii ASCII.
-dictionary ,-ascii
(1)
(2),.
lsearch
lsearch [-option] list pattern
list pattern-1option-exact glob -regexp,-glob
join
:join list [joinString]
list joinStringjoinString
split
split string [splitChars]
stringsplitChars splitChars
{}stringsplitChars ,
5file
TCL(
))
<1>I/O
pwdUNIX pwd
cd UNIX
tgrepTCLI/O
proc tgrep { pattern filename} {
set f [open $filename r]
while { [gets $f line ] } {
if {[regexp $pattern $line]} {
puts stdout $line
}
}
close $f
}
opengets,puts,close
TCL stdin,stdout stderr

<2>
gets readgets read
puts puts TCLseek,telleof

seek fileId offset [origin] fileIdoriginoffset


originstart current endstart
tell fileId fileId
eof fileId fileId10
<3>
grob
glob [switches] pattern [pattern ...]
globstring match

glob *.c *.h


glob */
file
file subcmd arg [arg...]
file subcmd
file exists name
file extension name
file mkdir dir [dir ...]
file copy [-force] [--] source target
file rename [-force] [--] source target
file delete [-force] [--] pathname [pathname ... ]

Tcl
1
Tcl 0x
TclANSI C
2
TclANSI C
Tcl
ANSI C

Tcl
TCLCifswitchforwhileforeachbreakcontinue
1
if { $x>0 } {
.....

}elseif { $x 1 } {
.....
}elseif { $x 2 } {
....
}else {
.....
}
'{' TCL if
Tcl
if {TCL'if{'

switch [option] $x {
ab {incr t1}
c {incr t2}
default {incr t3}
}
optionTCL-exact -globregexp,-glob-exact -globstring
match -regexp
a''default switch
switch
2
for init test reinit body
init test
reinit body
set b " "
for {set i [expr [llength $a] -1]} {$i>=0} {incr i -1} {
lappend b [lindex $a $i]
}
while a a b
set b " "
set i [expr [llength $a] -1]
while { $i>=0}{
lappend b [lindex $a $i]
incr i -1
}
foreach

1foreach varName list body


varNamelist ()body

set b " "


foreach i $a{
set b [linsert $b 0 $i]
}
2foreach varlist1 list1 varlist2 list2 ... body

Tcl
TCLTCL,TCLTCL
procTCLCTCL
proc
proc add {x y } {expr $x+$y}
proc
TCL proc
add 1 2 3
returnreturn

TCL

C
global
2

C\C++

TCLargs,
args
args
args

Anda mungkin juga menyukai