<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Wed Jun 19 00:13:49 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='393'>
<header><title>Add -command Option to lsearch</title><author address="mailto:pawelsalawa@gmail.com">Pawel Salawa</author><status type='project' state='draft' tclversion="8.7" vote='prior'>$Revision: 1.2 $</status><history></history><created day='25' month='apr' year='2011' /></header>
<abstract>This document describes new option for <emph style="bold">lsearch</emph> named <emph style="bold">-command</emph> that lets the developer define a custom comparision algorithm for searching for an element of a list.</abstract>
<body><section title="Rationale">
<para>The <emph style="bold">-command</emph> 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.</para>
<para>This is effectively introducing the <emph style="bold">-command</emph> option from <emph style="bold">lsort</emph> to <emph style="bold">lsearch</emph>.</para>
</section>
<section title="Specification">
<para>The option <emph style="bold">-command</emph> 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:</para>
<verbatim><vline encoding='base64'>ICAgIHByb2Mgc2VhcmNoQ29tbWFuZCB7bGlzdEVsZW1lbnQgcGF0dGVybn0gey4uLn0=</vline></verbatim>
<para>And used like:</para>
<verbatim><vline encoding='base64'>ICAgIGxzZWFyY2ggLWNvbW1hbmQgc2VhcmNoQ29tbWFuZCAkbGlzdCAkcGF0dGVybg==</vline></verbatim>
<para>The command must return a boolean value, where true means that the element matches the pattern. Since the <emph style="bold">-command</emph> option specifies a comparision method, therefor it&apos;s treated the same way as <emph style="bold">-exact</emph>, <emph style="bold">-glob</emph>, <emph style="bold">-regexp</emph> and <emph style="bold">-sorted</emph> options are. It also causes some options (e.g., <emph style="bold">-integer</emph>, <emph style="bold">-nocase</emph>) to be ignored.</para>
</section>
<section title="Reference Implementation">
<para><url ref="http://sqlitestudio.pl/tcl/patches/tip-393-lsearch-command.patch"/></para>
<para>Patch made against 8.6.0 beta.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
