Sei sulla pagina 1di 2

Tcl/Tk GUIDE puts $x ;#good comment

by Jason Bechtel #good comment Math:


The command is expr. Examples:
What is Tcl/Tk? I/O: set x [expr -3*4+5]
Tcl is a scripting language. That means that Standard I/O is performed with puts and gets: set x [expr ‘‘$a + $b’’]
it is best suited to high-level orchestration of eg: puts {‘‘Type your name: ‘‘} set x [expr sqrt($y)]
other programs and tasks. Tk is the graphical gets name
extension to Tcl that allows for the creation puts $name Procedures:
of graphical user interfaces. File I/O requires a pipe to be opened: The proc command sets up a procedure:
set fileid [open {filename} r] proc myfunc { arg1 arg2 } {
Where do I start? gets $fileid line }
Tcl is an interpreted language at heart. So, puts $line Then call it in a script just by typing the name
you need to install an interpreter in order to close $fileid of the procedure:
use it. The interpreter is called tclsh (or wish myfunc $x $y
for Tk). It can be used interactively or run on Quoting hell: Tip: You can store common functions off in
a Tcl/Tk (Tcl from now on) script that is Commands are interpreted in two passes. a file and use the source command to include
stored in an ASCII text file. You can obtain The first substitutes for variables. The them:
most information about Tcl from second evaluates the resulting command. source myfile.tcl
scriptics.com including how to download and Quoting using ‘{‘, ‘[‘, and ‘"’ control the
install the latest version of the interpreter. evaluation of the command. Curly braces More Variables:
Oh, and Tcl interpreters exist for Windows, disable substitution. Square brackets initiate Sharing variables between procedures:
UNIX, and Macintosh. a function call that performs a substitution proc myfunc { y } {
and then interprets the command. Quotes global outside_var
Can we just program already? allow substitution to occur. Then during upvar $y ref_var
We’ll start with commands for Tcl in general, evaluation, quotes and braces also work to }
which all apply in Tcl and Tk scripts. group words into a single argument. global causes outside_var to be evaluated in
the global scope. upvar causes y to refer to
Variables: set var {I’m a string} ref_var in the next scope level up.
set <variable> <value> puts {$var}
$<variable> gives the value in variable. > $var Structured programming:
value can be an int, float, or string. puts {‘‘$var’’} if {$x < $limit} ?then? {
eg: set x $y > ‘‘$var’’ script } elseif {$x == 10} {
eg: puts $x puts ‘‘{$var}’’ script } else {
> {I’m a string} script }
Commands and Comments: The backslash (\) is the escape character. while {![eof $fileid]} { }
A semi-colon or a carriage return separates Some common escape sequence strings: for {set x 0} {$x<$lim} {incr x} {}
commands. Comments begin with a ‘#’ \a audible alert (bell) foreach varname <list> {}
which must be preceded by a ‘;’ if it follows \f formfeed (clear screen) Lists:
a command on the same line. \n newline LISP anyone? In simplest form:
eg: set x 0 #bad comment \t tab set x {one two three}
set y {me you {buckle my shoe} } set strvar [join {a b 3 string}] the array can vary from index to index and
lindex x 2 > a b 3 string through time for a single index.
> three set listvar [split ‘‘cs.ua.edu’’ .] Examples:
lindex y 2 > cs ua edu set myArray(first) ‘‘NSF REU’’
> buckle my shoe set myArray(last) 1620
set z [list a b 3 string] Regular Expressions: set myArray(3) twelve
linsert x 1 foo; puts $x Regular expressions are supported by several set myArray(first) 4.78
> one foo two three functions. There are also the explicit Some additional commands:
lrange z 0 2 functions regexp and regsub. The simplest array exists arrayName
>ab3 general forms are array names arrayName ?pattern
lsort x; concat x z regexp exp string Returns a list of indices in no particular
> one three two a b 3 string regsub exp string subSpec varName order. Pattern restricts the list to indices
Expressions use the following notation: with values matching pattern.
Strings: ^ beginning of a string array size arrayName
Strings are a basic type, so it’s easy $ end of a string
set x {I’m a string}; set y string . any single character Between Arrays and Lists:
set x_len [string length $x] * any count 0-n of the previous pattern array get arrayName
string index $y 1 + any count 1-n of the previous pattern Returns a list in which each odd member
>t [] any char of a set of chars in of the list (1, 3, 5, etc) is an index into the
string range $x 2 5 [^] any char NOT in the set of chars associative array. The list element following
>mas () subSpec a name is the value of that array member.
string compare ‘‘foo’’ ‘‘bar’’ Examples: array set arrayName dataList
>1 set sample "Where there is a will" Converts a list into an associative array.
string compare ‘‘bar’’ ‘‘foo’’ set result [regexp {[a-z]+} $sample match] dataList is a list in the format of that returned
> -1 puts "$result match: $match" by array get.
string compare ‘‘foo’’ ‘‘foo’’ > 1 match: here
>0 set result [regexp {([A-Za-z]+) +([a-z]+)} \
string toupper ‘‘foobar’’ $sample match sub1 sub2 ] Where do I go from here?
> FOOBAR puts "Match: $match 1: $sub1 2: $sub2" There is a lot in Tcl without even getting into
string trim $y tr > Match: Where there 1: Where 2: there Tk. And there are a plethora of extensions
> sing regsub "will" $sample "lawsuit" sample2 that have been written for various specialized
Finally, C-style string formation: puts "New: $sample2" functions. I recommend going to
set x [format ‘‘%s%d%4.2f’’ str 2 pi] > Where there is a lawsuit scriptics.com for book recommendations.
> str23.14 Note: exps are subject to interpreter Also, download TclTutor by Clif Flynt. It
substitution. Escape characters accordingly. comes in handy.
Associative Arrays:
Between lists and strings: Tcl supports a very flexible array structure.
Lists can be turned into strings with the join Indices are indicated with strings rather than
command and strings can be chopped into numerals and the data type of the values in
lists with the split command:

Potrebbero piacerti anche