Main Content

User defined sorting and other uses of callbacks in Tcl and Tk

Archive - Originally posted on "The Horse's Mouth" - 2011-09-02 08:03:23 - Graham Ellis

When I output a table of results, I usually want it to be sorted in some way.

In Tcl, I can use lsort to sort a list - there's an example of it running in a default way [here]. However, there's often a need to order records according to a non-default algorithm, and there are switches such as -integer for cases like this.

  % set values "4 7 12 7 12 5 4"
  4 7 12 7 12 5 4
  % puts [lsort $values]
  12 12 4 4 5 7 7
  % puts [lsort -integer $values]
  4 4 5 7 7 12 12
  %


Good - but what about more complex cases? I can use the -command option to pass in to lsort the name of a command that takes two parameters and returns negative / zero / positive depending on whether the first parameter passed in is to come first in the sorted result set, if the two values are of the same value as far as sorting is concerned, or if the second parameter passed is to be first on the output.

The code for this can be quite short - there's an example from yesterday's Tcl course that I've uploaded [here]. But it can also be quite baffling for the newcomer, as it's often his / her first experience of what's known as a callback - that's where a call is made to a Tcl-provided command (lsort in this case) which then calls - possibly multiple times, and possibly later on the extra piece of user provided code.

Purely as a training demonstration of the guts of how this works, I re-implemented elements of lsort in my own proc called mysort and I have placed that code on our website [here].

Callbacks turn out to be very important in Tk - the Graphic User Interface that's often used with Tcl. When you set up a button, you'll usuaully tell Tcl/Tk what the button is to do when pressed via a callback. There's a shortish example [here] ...
  button .tog -text Marmite -command flip
... in which a used defined proc called flip is to be run whenever the button called .tog is pressed. The definition of the code to be run is done early in the setup; it may be run (if the button is ever pressed!) much later on, as described above. That's often referred to as a deferred callback.


Illustraton - the Tcl/Tk demonstration program in use.