Tcl Commands

 This is a list of the Tcl commands that I thought were good to start out with. If you notice any errors please e-mail me! They are arranged alphabetically. If you want a list of commands in Tcl just type ?. You can also get a list of arguments for most commands by simply typing in the command name (ex: lappend) with no arguments. ?argument? means it's optional. There are many, many more Tcl commands. If you want to see them, go to the man pages at http://www.scriptics.com/man

Basic Commands: (try these first)
after
append
array
break
expr
incr
lappend
lindex
list
llength
lrange
lsort
proc
puts
return
set
unset
vwait

Advanced Commands: (try 'em out! Just because I thought they were tougher, doesn't mean you will! Note that some of these may require you to understand concept covered in Advanced Tcl like pattern matching)
after
concat
join
linsert
lreplace
lsearch
split

 

after option argument(s) if after integer is run on its own, the console will halt for integer milliseconds (1/1000 of a second). You can add procedure names to this and the syntax would be something like: after 30000 Run_Me. 30 seconds after this was typed, Run_Me would be excecuted. In the mean time, it would be labelled after#1 (or #2 if there was already an after waiting). If cancel is used as an option, then you can kill these after#'s. Example: after cancel after#1. Other options include info (is the specified after# active?) and idle (used instead of a time, and waits until application is idle).

append variable value appends a value on to a variable. Common uses of this include append SomeInteger .0, which would have the effect of turning an integer into a real number.

array option arrayName variable(s) the array command is more easily understood as a series of commands, each having its own option.

 

array exists arrayName returns 1 if the variable (arrayName) is an array, otherwise it returns a 0.

 

array get arrayName pattern lists the pointers and values in an array that match the given pattern (see advanced Tcl for pattern matching), or all of them if no pattern is specified.

 

array names arrayName pattern lists the pointers (identifiers) that match the pattern, or all of them if no pattern is specified.

 

array set arrayName {id1 value1 id2 value2...} like a bunch of set commands run at once, it just sets the values to the pointers. This will overwrite previous assignments.

 

array size arrayName returns the number of elements in the array.

break normally used in loops, it "breaks" the loop and exits it, even if the terminating condition has yet to be met. This is usually considered clumsy programming, so use it sparingly.

concat list(s) combines all of the list(s) and returns one master list. Note it doesn't change list(s).

expr argument(s) the argument(s) can consist of any mathematical problem. Complexity can include parenthesis and mathematical properties are used to determine order (expr 1 + 2 * 3 will return 7). Note that expr can be used to generate random numbers! These are vital to games and simulations, and useless for most other things. To generate a random number from between 0 to 1 type expr rand() You'll have to do some fun math if you want to generate other random numbers, but this is the key.

incr variable amount adds amount to a variable. If no amount is specified, it adds one to the variable. Note that amount can be a negative number.

join list string returns a string comprised of the string inserted between each member of the list.

lappend listName value(s) appends values as items to the list.

lindex listName place returns the value at the specified place in the list.

linsert listName position value(s) inserts value(s) into listName at position and returns the new list. Doesn't change listName

list value(s) returns a list with the value(s) as elements.

llength listName returns the number of items in the list.

lrange listName FirstPlace LastPlace lists all the elements between and including Firstplace and LastPlace.

lreplace listName FirstPlace LastPlace value(s) returns the list created by replacing all the elements between and including positions FirstPlace and LastPlace with value(s). Note that the number of values need not be the same as the elements being replaced.

lsearch listName option pattern returns the elements in the list that match the pattern using the pattern-matching technique specified in option (-exact, -glob or -regexp). If no option is specified, -glob is used.

lsort SortingOption(s) listName returns the list sorted according to the options specified (these include -ascii, -command, -decreasing, -dictionary, -increasing, -index, -integer, or -real).

proc {variable(s)Passed} {body} see my notes on procedures in Core Tcl Rules.

puts nonewline(?) string puts a string to a screen. Doesn't put a newline after it if nonewline is specified.

return string does exactly what it says, returns a string (or the value of a variable, etc.). Used in procedures fairly often.

set variable value(s) assign the value to the variable. Create variable if it doesn't exist. If the variable is immediately followed by parenthesis surrounding a string, put the value into the array that that position. Note that lists must be created with the syntax: set listName [list value(s)].

split string removeString the opposite of join. Returns a list made up of the elements in the first string after every occurrence of removeString has been removed. Usually removeString is just one character, as multiple characters seem to add {}'s to the return.

unset variable(s) destroys the variable(s) listed. Can be used to destroy single elements in an array, or the entire array if no pointer is given.

vwait variable pause the script until the variable is written. Often used instead or in conjunction with after.

 

These are a few commands to play with, so do! Either skip ahead to the exercises and try out the Tcl only ones or better yet just open wish and start playing around. You can do an awful lot with what's on this page. When you're comfortable with some (if not all) of the commands, then let's move on to some Tk!

next page
contents
previous page


Copyright 1998 by Chris Palmer
Mail all comments to: Ardenstone@Ardenstone.com
Visit the rest of my Senior Seminar
or my homepage.