Version 12 of llength

Updated 2007-01-19 13:04:10

The man page for llength is http://purl.org/tcl/home/man/tcl8.4/TclCmd/llength.htm

This command returns the number of elements in the list. If the string is not a well-formed list, an error will be thrown.


caspian: Make sure to give llength the list, not just the name variable where the list is stored. For example:

 # This is the right way to do it.
 set mylist {a b c}
 llength $mylist

 # This is the WRONG way to do it.
 # This won't return an error, but it will always return "1",
 # no matter how long or short your list is.
 set mylist {a b c}
 llength mylist

This is one of those places where Tcl's treatment of bareword literals can be frustrating to someone.

In the second case above, Tcl considers the argument to llength a list. Therefore, it is going to return the length of the list, which is equivalent to this list:

        llength [list "mylist"]

DKF: The phrase "bareword literals" indicates a deviation from the Tcl Way; the concept does not really operate usefully in Tcl, unlike in a number of other languages (*cough*Perl*cough*). The llength command always works with list values, and lists are always values. If you've a list in a variable, you need to get the list out of the variable to work out its length.

GWM Note also the slightly unexpected result for:

  llength "a b c"  ;# returns 3 as the string (only one string) is interpreted as a 
        list by llength, the spaces making 3 elements. 
  llength "a {b d} c"  ;# is also 3 - the inner braces make a sub-list
  llength {a {b d} c}  ;# also 3 - it is the same as above
  llength "{a {b d} c}" ;# is 1 - the braces inside the string make a sub-list

Also not the difference between braced lists and quoted in this case:

  set a {a b c}
  llength "1 2 $a 3" ;# 6 elements in the substituted list
  llength {1 2 $a 3} ;# 4 elements in the list (which does not have $a substituted)

See also list, lappend, lindex, linsert, lrange, lreplace, lsearch, lsort .


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command