Version 9 of max

Updated 2008-09-19 12:39:42 by suchenwi

max is available as a function in expr or as a command in Tcl 8.5. Given a variable number of numeric arguments, it returns the one that is numerically largest.

 % expr max(2,3,4,5,6,-99)
 6

 % namespace import ::tcl::mathfunc::max
 % max 2 3 4 5 6 -99
 6

This does not work in releases up to 8.5.4 because max and min are not importable. This should be fixed in subsequent versions.

RS 2008-09-19: Before that, you can hot-patch it yourself in one line of code:

 namespace eval tcl::mathfunc namespace export max min ;# this is it
 namespace import tcl::mathfunc::max

If you are working with Tcl releases older than 8.5, it is not difficult to create procedures that provide the same capability.

Various ways: in a loop (works for numeric and string values):

 proc max list {
    set res [lindex $list 0]
    foreach element [lrange $list 1 end] {
       if {$element > $res} {set res $element}
    }
    set res
 }

By sorting (this is for numeric values only):

 proc max list {lindex [lsort -real $list] end}

See also Reinhard Max