Version 2 of mathematical set

Updated 2006-01-04 15:05:33 by escargo

4 Jan 2006 - Googie - Here's some very simple way to use set command like the following:

 set a 5
 set b $a + 3 + ($a * 2)
 puts $b

'$b' will be 18. The only requirement is to use 'mathematical' set case with more than 1 and 2 arguments (which are number of arguments for default set syntax). In practise it means that you have to do at least one white-space in math expression, or set will be interpreted as the Tcl-built-in one. Here's code to enable such functionality:

 rename ::set ::set.org
 proc ::set {var args} {
     if {[llength $args] > 1} {
         uplevel [list ::set.org $var [eval expr "{$args}"]]
     } else {
         uplevel [list ::set.org $var [lindex $args 0]]
     }
 }

RS: Cute. But the case of [llength $args]==0 should also be covered as returning the value - right now it would set it to {}. Also, all the efficiency gained with braced expressions is of course lost here...


Category Command