More list commands - lsearch, lsort, lrange

Table of Contents

More list commands - lsearch, lsort, lrange
Example

More list commands - lsearch, lsort, lrange

Lists can be searched with the lsearch command, sorted with the lsort command, and a range of list entries can be extracted with the lrange command.

lsearch list pattern

Searches

list for an entry that matches

pattern, and returns the index for the first match, or a -1 if there is no match. By default,

lsearch uses "glob" patterns for matching. See the section on

lsort list

Sorts

list and returns a new list in the sorted order. By default, it sorts the list into alphabetic order. Note that this command returns the sorted list as a result, instead of sorting the list in place. If you have a list in a variable, the way to sort it is like so:

set lst [lsort $lst]

lrange list first last

Returns a list composed of the first through last entries in the list. If first is less than or equal to 0, it is treated as the first list element. If last is end or a value greater than the number of elements in the list, it is treated as the end. If first is greater than last then an empty list is returned.