Version 1 of Multiple comparisons

Updated 2003-03-19 18:53:35

if 0 {Richard Suchenwirth 2003-03-18 - I needed a way for testing whether the elements in a list were all equal. Not a hard nut to crack - just compare the first with all the others. But then I thought on how to generalize this approach, so besides equality, it could also be used for testing monotonous ascension/descension, etc by comparing each two neighboring elements. Here's what I came up with, allowing the one-liner "luxury" of calling either with flat args, or a list (to avoid the need for eval ;-). Note that in order to pass the operator in as an argument, the if in the loop has an explicit expr invocation: }

 proc multicompare {op args} {
    if {[llength $args]==1} {set args [lindex $args 0]}
    set first [lindex $args 0]
    foreach i [lrange $args 1 end] {
       if {![expr $first $op $i]} {return 0}
       set first $i
    }
    return 1
 }

#--------------------------- Testing:

 % multicompare == 1 1 1 1
 1
 % multicompare == 1 1 1 1 0
 0
 % multicompare == 1 1 1 1 1.0
 1
 % multicompare == {1 1 1 1 1.0}
 1
 % multicompare < {1 2 3 4 5}
 1
 % multicompare < {1 2 3 4 5 0}
 0
 % multicompare < {1 2 3 4 5 6}
 1
 % multicompare <= {1 2 2 3 4 5 6}
 1
 % multicompare <= {1 21 2 3 4 5 6}
 0

Arts and crafts of Tcl-Tk programming