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.lsearchlistpattern- Searches
listfor an entry that matchespattern, and returns the index for the first match, or a -1 if there is no match. By default,lsearchuses "glob" patterns for matching. See the section on globbing. lsortlist- Sorts
listand 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] lrangelistfirstlast- Returns a list composed of the
firstthroughlastentries in the list. Iffirstis less than or equal to 0, it is treated as the first list element. Iflastis end or a value greater than the number of elements in the list, it is treated as the end. Iffirstis greater thanlastthen an empty list is returned.
Example
set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \{Madison 1809} {Monroe 1817} {Adams 1825} ]on*] incr x incr y -1set x [lsearch $list Washington*] set y [lsearch $list Madi s ;# Set range to be not-inclusive set subsetlist [lrange $list $x $y]subsetlist { puts "Starting in [lindex $item 1]: President [lindeputs "The following presidents served between Washington and Madison" foreach item $x $item 0] " } set x [lsearch $list Madison*] set srtlist [lsort $list] set y [lsearch $srtlist Madison*]lly"puts "\n$x Presidents came before Madison chronologically" puts "$y Presidents came before Madison alphabetica
No comments:
Post a Comment