TIP 303: Enhance 'llength' Command to Support Nested Lists

Author:		Wolf-Dieter Busch <[email protected]>
State:		Draft
Type:		Project
Tcl-Version:	9.1
Vote:		Pending
Created:	29-Jan-2007
Keywords:	Tcl, lindex


The command llength currently returns the length of a list. Sometimes it is desirable to know the length of a nested list. This TIP proposes an improvement to llength to do that.


Currently, finding the length of a nested list requires the combination of llength and lindex. This works, but is not very clean for a comparatively common operation when you compare with straight lindex usage. This TIP proposes to enhance llength so that it also does the indexing, making it's usage in such situations cleaner and less subject to programming errors.

Proposed Change

The llength command shall be updated to have syntax like this:

llength list ?indexList ...?

When no indexList is supplied, the current behavior is used. When one or more indexList arguments are supplied, they are used to restrict which part of list is taken the length of, just as if lindex had been used to index into list.

Thus, [llength {a {b c} d}] shall return 3 as nowadays, [llength {a {b c} d} 1]> shall return the length of the nested list on index 1, here {b c} => 2, and [llength {a {b c} d} 1 0] shall return the length of the nested list on index 0 of nested list on index 1, i.e., 1 in this case.


As this only changes a way of use of the llength that currently returns an error, there are no compatibility problems.

Example Implementation

The procedure idxllength (below) does what is described above, but inefficiently:

proc idxllength args {
    llength [lindex {*}$args]


This document has been placed in the public domain.