max

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

or use the full qualified name:

% ::tcl::mathfunc::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 args {
    set res [lindex $args 0]
    foreach element [lrange $args 1 end] {
        if {$element > $res} {set res $element}
    }
    return $res
}

By sorting (this is for numeric values only):

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

DKF: It was also a command and function in TclX.


See also Reinhard Max