Lists Are Strings, but Not All Strings Are Lists

Tcl’s only data type is the string, and each command can interpret strings in special ways.[6] A list is a special interpretation of a string—a list of words separated by whitespace. Lists are a very powerful feature of Tcl: they are easy to visualize and can be formed from simple strings. This example creates the variable name from a string and then causes the string to be interpreted as a list with lindex:

set names "bob carol ted alice"
puts [lindex $names 2]

This code produces the following output:

ted

Trouble begins when lists are assembled from arbitrary strings that may contain special Tcl characters. For example, suppose you are writing a program to count the number of words in each line of a file. You notice that Tcl has an llength command, which returns the number of words in a list, and decide to use it:

set fd [open $somefile]
gets $fd aLine
while {! [eof $fd]} {
    puts "line has [llength $aLine] words"
    gets $fd aLine
}
close $fd

You start running your program, and all is well until you read a file that contains:

Tcl has several quoting characters, which
include { to mark the beginning of a fully
quoted string, up to a matching }.

Your program then fails with “unmatched open brace in list.” The opening brace in the second line is interpreted as the beginning of a quoted string, possibly a list itself.

The key is to not use list commands on arbitrary strings, and use only list commands to build lists. Tcl even includes a list command that builds properly quoted lists from various strings. The first example in this section can be built as follows:

set names [list bob carol ted alice]

The list command is also very useful for building Tcl commands to be executed at a later time, helping to ensure that a command contains the expected number of arguments (see “Common Tk Errors,” later in this chapter, for an example).

To add to an existing list, use lappend. Like list, lappend ensures that strings are made into proper list elements as they are appended:

lappend names arnold beth
set newList [lreplace $names 2 3 george susan]
puts [lsort $newList]

This code produces the following output:

arnold beth bob carol george susan

Lists can be nested. Any list element that is itself a list is properly handled as one element during list processing on the outermost level. Extended Tcl (see Chapter 10, TclX) adds a data structure known as a keyed list, which mimics structures in C. A keyed list is a list made up of pairs of key identifiers followed by data. Ordinary Tcl list commands can pick apart keyed lists, but the keyed list commands in TclX make the job much easier and more efficient.

Strings are best manipulated with the Tcl commands string, regexp, regsub, scan, format, append, and subst. The split command can be used to make a string into a list while properly quoting any troublesome list elements.



[6] Beginning with Tcl 8.0, data types also have an internal representation as string, integer, float, and list, but to the programmer all data types are still strings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset