I have two list, let say X and Y. Find the highest values of List X, and compare it with the values of List Y, If values of Y greater then X, then add it in new list using TCL.
set X[list 1.2156476714e-04 1.1284486163e-03 1.9818406145e-01 2.9287846814e-04 2.0217831320e-04]
set Y[list 1.2156476714e-04 1.1284486163e-03 4.5386226702e-02 4.4706815970e-02 8.4928524302e-03 6.0775778365e-03 3.1041158763e-03 1.5045881446e-01 4.1016753016e-04 1.1655993148e-03 1.8736355969e-03 2.9444883694e-02 2.5420340535e-02 2.0819682049e-03 9.5297318694e-03 8.5498101043e-04 1.5580140825e-02 8.0796216935e-03 4.8684447393e-02 1.6464813962e-01]
take the highest value of List X, compare it with each values of List Y. If values of List Y greater than X value add it in new list.
Finding the maximum value of things is a matter for the max()
function (in expr
) except we want to apply it to a list so we instead call the function's implementation command directly so we can feed in the list of values via expansion:
set max_X [tcl::mathfunc::max {*}$X]
# ^^^^^^^^^^^^^^^ ^^^ Expansion: pass what follows as many arguments
# Function calls get transformed into invokes of commands in the tcl::mathfunc namespace
For the filtering of the second list, it is clearest to write a procedure. The filtering can be implemented with either foreach
or lmap
; the latter is really just a foreach
that collects the values if they're normal results and not something like a continue
.
These two versions of the procedure do essentially the same thing:
proc filter_greater_than {value list} {
lmap x $list {expr {$x > $value ? $x : [continue]}}
}
proc filter_greater_than {value list} {
set result {}
foreach x $list {
if {$x > $value} {
lappend result $x
}
}
return $result
}
You then use the procedure like this:
set new_list [filter_greater_than $max_X $Y]