Anand
Anand

Reputation: 1122

Tcl: on the fly command substuition

I have the following code which based on the variable op invokes either min or max.

  switch $op {
      min {
        set x  [::tcl::mathfunc::min {*}$lax]
        set y  [::tcl::mathfunc::min {*}$lay]
      }
      max {
        set x  [::tcl::mathfunc::max {*}$lax]
        set y  [::tcl::mathfunc::max {*}$lay]
      }
  }

Instead of writing it through a switch, I want to write something like the following.

       set x  [::tcl::mathfunc::$op {*}$lax]
       set y  [::tcl::mathfunc::$op {*}$lay]

I tried using subst command but could not get it to work. I use Tcl 8.5.7

Upvotes: 1

Views: 364

Answers (1)

kostix
kostix

Reputation: 55453

Your second example should work unmodified, so no need to over-engeneer. Observe:

% set lax {1 2 3}
1 2 3
% set op min
min
% set x [::tcl::mathfunc::$op {*}$lax]
1
% set op max
max
% set x [::tcl::mathfunc::$op {*}$lax]
3

Upvotes: 6

Related Questions