TIP #303: ENHANCE 'LLENGTH' COMMAND TO SUPPORT NESTED LISTS ============================================================= Version: $Revision: 1.3 $ Author: Wolf-Dieter Busch State: Draft Type: Project Tcl-Version: 8.7 Vote: Pending Created: Monday, 29 January 2007 URL: https://tip.tcl-lang.org303.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== 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. DESCRIPTION ============= 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. COMPATIBILITY --------------- 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] } COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows