Version 5 of max

Updated 2007-11-01 15:00:56 by dgp

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::mathfun::max
 % max 2 3 4 5 6 -99
 6

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