Version 4 of max

Updated 2007-11-01 12:57:45 by LV

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

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

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}

In Tcl 8.5, the expr command has a max() function.


See also Reinhard Max