Version 1 of listify

Updated 2010-02-24 15:13:55 by lars_h

In Tcl, to listify a string is the process the interpreter goes through to create a list object whose contents are in a list safe form.

The split command can be used by a script to force the change.

Lars H thinks the above is under-specified to such a degree that it is meaningless, and most definitely confusing. It is true that split always returns a list, but that doesn't mean it is some universal listification operation, or even that the idea of such a thing makes sense. Every string->list operation makes some sort of interpretation of the string, and it is generally that interpretation one should focus on specifying, not just wanted input and output datatypes. split applies one interpretation which is sometimes appropriate, and other times quite wrong. It is in particular rather far from preserving input that is already a list, which might be one requirement for a listify operation.

An example (based on some comments left on the lset page) of listification of a Tcl variable is:

% set l "    a    b    c }   d   "
    a    b    c }   d   
% lset l 1 a
a a c \} d
% lindex $l 2
% puts $l
a a c \} d

Notice that leading spaces are gone, internal spaces are collapsed into 1, and the right brace is quoted.

Also, note that strings with certain characters can result in errors rather than listification:

% set l "  a  b     c    }  d { e "
  a  b     c    }  d { e 
% lset l 0 "  a"
unmatched open brace in list

Using split to force the listification results in completion of the task:

% set l "  a  b     c    }  d { e "
  a  b     c    }  d { e 
% set a [split $l]
{} {} a {} b {} {} {} {} c {} {} {} \} {} d \{ e {}
% lset a 0 "  a"
{  a} {} a {} b {} {} {} {} c {} {} {} \} {} d \{ e {}