Add -command Option to lsearchPawel Salawa$Revision: 1.2 $
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:
ICAgIHByb2Mgc2VhcmNoQ29tbWFuZCB7bGlzdEVsZW1lbnQgcGF0dGVybn0gey4uLn0=
And used like:
ICAgIGxzZWFyY2ggLWNvbW1hbmQgc2VhcmNoQ29tbWFuZCAkbGlzdCAkcGF0dGVybg==
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.
Patch made against 8.6.0 beta.
This document has been placed in the public domain.