Data Structures

Tcl has two structures, lists and arrays.

Lists

Lists are just strings with whitespace separating elements.

This is table 6.1 from Ousterhout:
concat ?list list ...?

Joins multiple lists into a single list (each element of each list becomes an element of the result list) and returns the new list.

join list ?joinString?

Concatenates list elements together with joinString as separator and returns the result. joinString defaults to a space.

lappend varName value ?value ...?

Appends each value to variable varName as a list element and returns the new value of the variable. Creates the variable if it doesn't already exist.

lindex list index

Returns the index'th element from list (0 refers to the first element).

linsert list index value ?value ...?

Returns a new list formed by inserting all of the value arguments as list elements before index'th element of list (0 refers to the first element).

list ?value value ...?

Returns a list whose elements are the value arguments.

llength list

Returns the number of elements in list.

lrange list first last

Returns a list consisting of elements first through last of list. If last is end, it selects all elements up to the end of the list.

lreplace list first last ?value value ...?

Returns a new list formed by replacing elements first through last of list with zero or more new elements, each formed from one value argument.

lsearch ?-exact? ?-glob? ?-regexp? list pattern

Returns the index of the first element in list that matches pattern or -1 if none. The optional switch selects a pattern-matching technique (default: -glob).

lsort ?-ascii? ?-integer? ?-real? ?-command command? ?-increasing? ?-decreasing? list

Returns a new list formed by sorting the elements of list. The switches determine the comparison function and sorted order (default: -ascii -increasing).

split string ?splitChars?

Returns a list formed by splitting string at instances of splitChars and turning the characters between these instances into list elements.

List examples

  1. Lists are just strings: set listOne {cat dog cow} set listTwo [list cat dog cow] put $listOne\n$listTwo outputs: cat dog cow cat dog cow
  2. The length of a list: puts [llength [list cat dog cow]] outputs: 3
  3. Sorting a list: puts [lsort [list cat dog cow]] outputs: cat cow dog

Arrays

Arrays in Tcl are actually hash tables, their keys can be anything. Arrays do not have a value themselves, only their components. Note that the index into the array must be one word. Some useful commands for dealing with arrays:
array names name

Returns a list containing the names of all the elements of array name.

array size name

Returns a decimal string giving the number of elements in array name.

Array examples

There are no special commands to create arrays: set anArray(first) foo set anArray(second) bar set anArray(third) baz # note that we do not use $anArray below, # arrays themselves do not have value array size anArray array names anArray outputs: 3 second first third Naturally, you cannot rely on the result of array names to be consistent across calls.

Multidimensional arrays

Since the index of the array can be anything, these are easy: set anotherArray(1,1) 1 set anotherArray(1,2) 2 set anotherArray(2,1) 3 set anotherArray(2,2) infinity puts [array names anotherArray] outputs: 2,2 1,1 2,1 1,2 The only requirement is that the index is just one word.

Using arrays as structures

Just use the keys for indexes: set person(name) "Clinton Roy" set person(nick) latte set person(shell) bash
Previous Contents Next
Clinton Roy