| |||||||||||
| |||||||||||
Sorting in Tcl - lists and arrays
Tcl's lsort command lets you sort a list - and that can be a list of the keys of an array. You can't sort the array, but once you have the list of keys you can sort that and use it to iterate through the array in any order that you like.
As everything (except an array) is a string in Tcl, when you sort a list the language defaults to an asciibetic sort - which you can vary with the -integer or -real options to lsort. And if you want to specify another criterion, you can do that too, using the -command option to specify the name of a proc that returns -1, 0 or +1 to indicate the ordering of two input records. There's examples of all three following - I'm sorting a list of keys from an array asciibetically, numerically, and by the value that the key points to: proc report {order say} {Here are the results: earth-wind-and-fire:~/oct07/camb grahamellis$ tclsh allsorts(written 2007-10-24 20:05:46) Associated topics are indexed under T208 - Tcl/Tk - ArraysT206 - Tcl/Tk - Lists
Some other Articles
What is Expect?Wireless hotel tips - FTP and Skype connections failing Reading from another process in Tcl (pipes and sockets) Away or home - which do I prefer? Sorting in Tcl - lists and arrays Tcl - global, upvar and uplevel. Square Bracket protection in Tcl Tcl - append v lappend v concat Tcl - using [] or {} for conditions in an if (and while) Dark Dawn 1690 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||