Updated 2017-06-24 16:50:28 by WJG

WJG (24/06/17) Given a list of integers which ones are closest to some target value?
#---------------
# determine which items from a list of integers are closest to a target value
#---------------
# Args:
#        arrows
#        target
# Returns:
#   indices of values in the list which are closest to the target value
#
proc closest_hit { arrows target} {
        
        set least $target
        set res ""
        
        for { set i 0 }  { $i < [llength $arrows] } { incr i } {
                        
                set diff [expr abs($target - [lindex $arrows $i])]
                
                if { $diff == $least } { lappend res $i }
                
                if { $diff < $least} {
                        set least $diff
                        set res $i
                } 

        }
        
        return $res
} 


puts >>>>[closest_hit "5 11 6 12 20 30 11" 10]