TIP #80 Version 1.3: Additional Options for 'lsearch'

This is not necessarily the current version of this TIP.


TIP:80
Title:Additional Options for 'lsearch'
Version:$Revision: 1.3 $
Authors: Tom Wilkason <tom dot wilkason at home dot com>
Tom Wilkason <tom dot wilkason at cox dot net>
State:Draft
Type:Project
Tcl-Version:8.4
Vote:Pending
Created:Wednesday, 02 January 2002
Discussions To:news:comp.lang.tcl

Abstract

This TIP proposes additional options for the lsearch command to return and work with all matching items in the return rather than the first matching item.

Rationale

The lsearch function works well for finding the first item in a list that matches a pattern. However it is often useful to find all of the items in the list that match a pattern. This TIP proposes adding options to return the entire list of matches. With this capability, additional options are proposed to return the data rather than the indices (since you often want to work the the data anyway), and to add an option to return the logical exclusion of the matching items (i.e. those that don't match the search pattern).

Specification

I propose the following options be added to lsearch:

Option: -start index

Initiates the list search starting at index, which can be any valid list index (such as 0 , end , end-1 ...)

Option: -all

Returns a list of all indices that match the search condition (rather than the first one). The indices are returned low to high order. For a no match condition, a {} (empty result) is returned. If the the -all or -data switches are not specified, a -1 is returned for a no match condition just as it is done now.

Option: -data

Returns a list of the data that matches the search condition rather than the index (or indices with the -all option). A {} is returned for a no match condition. The data is returned in proper list order. This option is usefu