Tcl Lists

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 5.  Tcl Lists


A Tcl list is a sequence of values. When you write out a list, it has the same syntax as a Tcl command. A list has its elements separated by white space.Braces or quotes can be used to group words with white space into a single list element. Because of the relationship between lists and commands, the list-related commands described in this chapter are used often when constructing Tcl commands.

graphics/tip_icon.gif

Big lists were often slow before Tcl 8.0.


Unlike list data structures in other languages, Tcl lists are just strings with a special interpretation. The string representation must be parsed on each list access, so be careful when you use large lists. A list with a few elements will not slow down your code much. A list with hundreds or thousands of elements can be very slow. If you find yourself maintaining large lists that must be frequently accessed, consider changing your code to use arrays instead.

The performance of lists was improved by the Tcl compiler added in Tcl 8.0. The compiler stores lists in an internal format that requires constant time to access. Accessing the first element costs the same as accessing any other element in the list. Before Tcl 8.0, the cost of accessing an element was proportional to the number of elements before it in the list. The internal format also records the number of list elements, so getting the length of a list is cheap. Before Tcl 8.0, computing the length required reading the whole list.

Table 5-1 briefly describes the Tcl commands related to lists.

Table 5-1. List-related commands.
list arg1 arg2 ...Creates a list out of all its arguments.
lindex list iReturns the ith element from list.
llength listReturns the number of elements in list.
lrange list i jReturns the ith through jth elements from list.
lappend listVar arg arg ...Appends elements to the value of listVar.
linsert list index arg arg ...Inserts elements into list before the element at position index. Returns a new list.
lreplace list i j arg arg ...Replaces elements i through j of list with the args. Returns a new list.
lsearch ?mode? list valueReturns the index of the element in list that matches the value according to the mode, which is -exact, -glob, or -regexp. -glob is the default. Returns -1 if not found.
lsort ?switches? list

Sorts elements of the list according to the switches: -ascii, -integer, -real, -dictionary, -increasing, -decreasing, -index ix, -command command.

Returns a new list.

concat list list ...Joins multiple lists together into one list.
join list joinStringMerges the elements of a list together by separating them with joinString.
split string splitCharsSplits a string up into list elements, using the characters in splitChars as boundaries between list elements.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net