|
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} {
puts $say
global stop
foreach pl $order {
puts "$pl: $stop($pl)"
}
}
#
proc byp {a b} {
global stop
return [string compare $stop($a) $stop($b)]
}
#
set stop(1) Bath
set stop(2) Bristol
set stop(6) Keynsham
set stop(3) Easterton
set stop(4) unused
set stop(5) unused
set stop(10) London
set stop(11) "The West"
#
set bystop [lsort [array names stop]]
set properbystop [lsort -integer [array names stop]]
set byplace [lsort -command byp [array names stop]]
#
report $bystop "By Ascii stop"
report $properbystop "By numeric stop"
report $byplace "By place name"
Here are the results:
earth-wind-and-fire:~/oct07/camb grahamellis$ tclsh allsorts
By Ascii stop
1: Bath
10: London
11: The West
2: Bristol
3: Easterton
4: unused
5: unused
6: Keynsham
By numeric stop
1: Bath
2: Bristol
3: Easterton
4: unused
5: unused
6: Keynsham
10: London
11: The West
By place name
1: Bath
2: Bristol
3: Easterton
6: Keynsham
10: London
11: The West
4: unused
5: unused
earth-wind-and-fire:~/oct07/camb grahamellis$ (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 failingReading from another process in Tcl (pipes and sockets)Away or home - which do I prefer?Sorting in Tcl - lists and arraysTcl - global, upvar and uplevel.Square Bracket protection in TclTcl - append v lappend v concatTcl - using [] or {} for conditions in an if (and while)Dark Dawn
|
1892 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, 35, 36, 37, 38 at 50 posts per page
This 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).
|
|