[Richard Suchenwirth] 2003-02-12 - The following little state machine was created, with help from [dkf], in the [Tcl chatroom], instigated by the quote (Alan Cox?): "A computer is a state machine. Threads are for people who can't program state machines." So here is one, starting with the necessary state transition command - [goto in Tcl]. } proc goto label { upvar 1 nextstate nextstate set nextstate $label return -code continue } if 0 {The "machine" itself takes a paired list of labels and state code; if a state code does not end in a ''goto'', the same state will be repeated as long as not left, with ''goto'' or [break]. Execution starts at the first of the states. } proc statemachine states { array set state $states set nextstate [lindex $states 0] while 1 {eval $state($nextstate)} } # Testing: a tiny state machine that greets you as often as you wish: statemachine { 1 { puts "how often?" gets stdin nmax if {$nmax==""} break set n 0 goto 2 } 2 { if {[incr n] > $nmax} {goto 1} puts "hello" } } ---- [Category Concept] | [Arts and crafts of Tcl-Tk programming]