source ./file.tcl
set foo 5
puts $foo
expr 4 + 5 expr 1 / 3.0 expr 2 * asin(1.0)
set foo [ expr 3 + 7 ] set pi [ expr 2 * asin(1.0) ]
if { $x == 0 } {
puts "Error zero division"
} else {
set z [ expr $y / $x ]
}
set i 0; while { $i<10 } incr i
set i 0
while { $i<10 } {
puts $i
incr i
}
for { set i 0 } { $i < 10 } { incr i 3 } {
puts $i
}
set i 1
foreach value { 2 3 4 5} {
set i [expr $i * $value] }
switch -exact $value {
foo { puts "foo $value" }
bar { puts "$value bar" }
default { puts $value }
}
proc foo { a b } {
puts "$a $b" }
proc bar { a b {c 5} } {
puts "$a $b $c" }
proc fooN { a b args } { ( args est un mot-cle)
puts "$a $b"
puts $args
}
foo 2 3 --> 2 3 foo 1 ERREUR ; no value given for parameter "b" to "foo" bar 1 2 3 --> 1 2 3 bar 1 2 --> 1 2 5 fooN 1 2 3 4 5 --> 1 2 3 4 5 fooN 1 2 3 --> 1 2 3 fooN 1 2 --> 1 2
set foo 5
proc printfoo { } {
global foo
puts $foo
}
string length "abc" --> 3 string range abcdefg 1 4 --> bcde set foo "abc" append $foo d e f --> abcdef
string match a* "alpha"
--> 1
string match {[ab]*} abricot
--> 1
string match {[ab]*} hello
--> 0
regexp a+ baaacaa --> 1 regexp a+ bbbbbbb --> 0
set foo { 1 2 3 }
--> 1 2 3
set x 4 ; set y 7
list $x $y
--> 4 7
lindex $foo 1
--> 2
concat $foo { a b }
--> 1 2 3 a b
set TAB(chien) dog puts $TAB(chien) set TAB(chat) cat ; set TAB(oiseau) bird array size TAB --> 3 array names TAB --> chat oiseau chien
puts "abc" puts stdout "abc" (affiche a l'ecran) puts $id "abc" (affiche sur un canal de sortie, cf. plus bas) gets stdin (lecture au clavier, ramene le nb de caracteres lus) gets stdin toto (lit au clavier et met le resultat dans toto) gets $id toto (la meme chose avec un canal d'entree)
set id [ open "foo.tcl" r ] puts [gets $id] (lit une ligne) close $id
set id [ open "foo.tcl" r ]
set content [ read $id ] (lit tout le contenu)
puts $content
close $id
while {[gets $id line] >= 0 ] {
process $line
}
foreach line [split [read $id]] {
process $line
}
set d [exec date] set n [exec sort <file | uniq | wc -l ] exec my_shell_program $x $y
if [catch { open ./exo.tcl r} id] {
puts "Erreur : $id"
} else {
set content [ read $id ]
close $id
}
proc savediv { a b } {
if { $b == 0 } {
error "division par zero impossible"
} else {
return [ expr $a / $b ]
} }
if [catch { savediv 5 0 } x] {
puts "Attention : pas de division par zero"
} else {
puts $x
}