Version 1 of comb

Updated 2004-06-10 18:34:00

Richard Suchenwirth 2004-06-10 - A little challenge in Tcl programs for beginners made me rebake an earlier half-baked idea: the pattern of a data comb which returns some string depending on a numeric value and a specification, which gives the possible strings and the borderline values, e.g. (in Centigrades):

 {"bitter cold" -10 cold 10 fresh 15 nice 25 warm 30 hot}

You can consider the even elements of this list as being the comb's teeth, and the even elements as the results to return when the tested value lies between two teeth (or outside). The code is of course pretty easy:

 proc comb {x spec} {
    foreach {value limit} $spec {if {$x<$limit} {return $value}}
    return $value
 }

# Testing:

 % comb 11 {child 13 teen 19 adult}
 child
 % comb 15 {child 13 teen 19 adult}
 teen
 % comb 99 {child 13 teen 19 adult}
 adult

Arts and crafts of Tcl-Tk programming