Previous
String Manipulation 
Next
If 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Lists

Lists

A Tcl list holds a sequence of elements, each of which can be numbers, strings, or other lists. Lets make a list :

set list_name "To Do List"
# Create an Empty list using the list command. Not very nessary  
set to_do [list]
#Add items to our to do list
lappend to_do "Buy Groceries"
lappend to_do "Tame Nature"
lappend to_do "Split Atom"

#Display all things
label .desc -text "The $list_name\n$to_do"
label .one -text "First thing to do : [lindex $to_do 0]" ;#Get first element
label .second -text "Second thing to do : [lindex $to_do 1]";#Get second element
label .third -text "Third thing to do : [lindex $to_do 2]" ;#and so on
pack .desc .one .second .third

#Insert a new Item in the second place
set to_do [linsert $to_do 1 "Capture Osama Bin Laden"]
label .list -text "New List\n$to_do"

#Change an item
set to_do [lreplace $to_do 3 3 "Someone already did that"]
label .latest -text "Latest List\n$to_do"
label .total -text "Total number of jobs to do = [llength $to_do]"
pack .list .latest .total
The commands used here for lists and their descriptions:
CommandLine SyntaxDescription
list3list ?value value value ...? This command returns a list comprised of all the args, or an empty string if no args are specified.
lappend5lappend listName ?value value value ...? This command treats the variable given by listName as a list and appends each of the value arguments to that list as a separate element, with spaces between elements. If listName doesn't exist, it is created as a list with elements given by the value arguments.
lindex10lindex list ?index...? The lindex command takes a Tcl list and shows its index'th element. Remember that lists start at 0 and the first element is 0'th one.
linsert17linsert list index element ?element element ...? This command produces a new list from list by inserting all of the element arguments just before the index'th element of list.
lreplace21lreplace list first last ?element element ...? lreplace returns a new list formed by replacing one or more elements of list with the element arguments. first and last in the syntax specify the first and last index of the range of elements to replace
llength23llength list Treats list as a list and returns a decimal string giving the number of elements in it.

There are more list related commands like lsort, lset, lrange, lsearch and even others like split, join etc. See the manual for details on these commands.

split
Split is a very useful command for creating lists from strings. If you want to split a string into several items at any character in the string you can use the... em... split command. If you want to get every word into a list in a sentence this command is very useful.
set words [split $sentence " "]
Here the variable called $sentence is cut at each and every instance of space in it - essentially splitting it into words. Now there is a new list called $words. The result of [lindex $words 0] will the first word, 1 will be second and so on. Another useful thing one can do with this command is to read a file with the read command and split the resulting string with "\n" to get a list of all lines in the file. You need to get to File Handling to learn that.


Arrays

One can make another kind of array in Tcl with the "array" command. This gives each value in the array a name and the values can be accessed with this name.

Syntax:
array option arrayName ?arg arg ...?

I like this syntax better. Make it a lot more readable...
array option arrayName {
arg arg
...?
}

#Make the array
array set star_trek {
	location	"Bridge of the Enterprise"
	problem		"Power surge"
	solution	"a fuse"
}
#Access the variables
label .sol1 -text "The $star_trek(problem) at $star_trek(location) \
	could have been avoided if they had $star_trek(solution)."
#Change the variable
set star_trek(solution)	"Wesley"
#Access the changed variables
label .sol2 -text "The $star_trek(problem) at $star_trek(location) \
	could have been avoided if they had $star_trek(solution)."
#Show the solutions
pack .sol1 .sol2

The command 'array set star_trek' creates the array and gives the following arguments as its contents. It can be access with this code
$<ARRAY_NAME>(<ELEMENT_ID>)
It could be changed just as easily by removing the '$' sign as when accessing any other variable. Eg:
set <ARRAY_NAME>(<ELEMENT_ID>) <VALUE>.

Some 'array' related commands and their descriptions:
CommandSyntaxDescription
array set array set arrayName { list } Sets the values of one or more elements in arrayName. list must have a form like that returned by array get, consisting of an even number of elements. Each odd-numbered element in list is treated as an element name within arrayName, and the following element in list is used as a new value for that array element.
array size array size arrayName Returns a decimal string giving the number of elements in the array. If arrayName isn't the name of an array then 0 is returned.
array namesarray names arrayName ?mode? ?pattern? Returns a list containing the names of all of the elements in the array that match pattern. Mode may be one of -exact, -glob, or -regexp.
Previous
String Manipulation 
Next
If 
Subscribe to Feed