Notas de Tcl: * Everything is a string. * set name Neftali set frase "Hola soy Neftali" set p "My pid is [pid]" * puts "Hello world" puts "The value of name is $name" * [expr 1+1] incr i 1 incr i -2 * while {expression} { ... } * for {start} {expression} {next} { ... } * if {condition1} { ... } elseif {condition2} { ... } else { ... } * switch -- $count \ 1 { ... } 2 { ... } 3 { ... } * Continuation lines: \ {same line inside} "same line inside" * break, continue * proc fib {pen ult n} { ...return... } puts [fib pen ult n] * pass by value pass by reference global pi,e * source to include external files. * expr sirve para evaluar expresiones true(1) false(0). (0x0-->hexadecimal, 0-->octal) expr {$count > 0} if {"0x0"=="0"} {puts equal} * llength lindex lrange * foreach element $list { puts $element } * varying argument lists proc p1 {a b args} args is set to a list * lists of lists set {a b {c d} {e {f g {xyz}}}} * Creating list list concat lappend linsert $list 0 new lreplace $list 1 3 x y lsearch -exact {a b c d ?} "?" lsort $list split $line ":" join $line "/" * set format "%1c%_8s==%f" ... scan $x "%c%8s%*\[=]%f" ... * string compare $a $b string match "*.c" "main.c" string first "uu" "uunet.uu.net" string last "uu" "uunet.uu.net" string length "foo" string index "abcde" 2 string range "abcde" 2 3 string tolower "Next" string toupper "Next" string trimleft $num "-" string trimright $num "-" string trim $num "-" * append * Arrays: uid(i), uid(user,shell) * info exist variable_name info locals variable_name info globals variable_name info vars variable_name info commands info procs info script array size array names * unset a unset array * trace * if [catch divalot] { puts "got an error in divalot!" exit } * catch set errorInfo * error * eval $cmd * upvar (passing by reference) * Ficheros externos open "/etc/passwd" "r" ("w","a") set input [open "/etc/passwd" "r"] (stdout,stdin,sterr) close $input (flush $file) while 1 { if {[gets $file line]==-1} break #do something with $line } Ej1_mio: #!/usr/bin/tclsh set file1 [open "fuerzas" "r"] set file2 [open "salida" "w"] while 1 { if {[gets $file1 line]==-1} break puts $file2 [split $line ":"] } Ej2_mio: #!/usr/bin/tclsh set file [open "fuerzas" "r"] foreach line [split [read $file] "\n"] { split $line ":" } * foreach filename [glob *.f] { set file [open $filename] #do something with file close $file } * file dirname tail extension rootname exist size * exec