TIP #393: ADD -COMMAND OPTION TO LSEARCH ========================================== Version: $Revision: 1.2 $ Author: Pawel Salawa State: Draft Type: Project Tcl-Version: 8.7 Vote: Pending Created: Monday, 25 April 2011 URL: https://tip.tcl-lang.org393.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== 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. RATIONALE =========== 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*. SPECIFICATION =============== 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. COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows