TIP 393: Add -command Option to lsearch

Author:         Pawel Salawa <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        25-Apr-2011
Tcl-Version:    9.1
Implementation-URL: http://sqlitestudio.pl/tcl/patches/tip-393-lsearch-command.patch


This document describes new option for lsearch named -command that lets the developer define a custom comparision algorithm for searching for an element of a list.


The -command option would be very useful when someone wants to look for element on list, that contains objects (TclOO, Itcl, etc), or simply Tcl dicts. Specifying a command lets a developer to decide what data from objects in a list is important for the comparison.

This is effectively introducing the -command option from lsort to lsearch.


The option -command takes additional argument, which is a name of a command prefix that implements the comparison. The command prefix will have two extra arguments added during evaluation, being (1) the element of the list and (2) the pattern that is being sought. Thus, a suitable command might be defined like:

    proc searchCommand {listElement pattern} {...}

And used like:

    lsearch -command searchCommand $list $pattern

The command must return a boolean value, where true means that the element matches the pattern. Since the -command option specifies a comparision method, therefor it's treated the same way as -exact, -glob, -regexp and -sorted options are. It also causes some options (e.g., -integer, -nocase) to be ignored.

Reference Implementation


Patch made against 8.6.0 beta.


This document has been placed in the public domain.