在解决迭代创建包含公式的列时,我遇到了需要以一种我不明白为什么的方式来构建解决方案的问题。我最初的解决方案是
Names Default To Here( 1 );
dt =
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );
colNames = dt << get column names( continuous, string );
For Each( {col}, colNames,
theTarget = (Column( Eval( col ) ) << get property( "Spec Limits" ))["Target"];
Eval(
Eval Expr(
New Column( col || " Minus Target",
Numeric,
"Continuous",
Format( "Best", 10 ),
Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
)
)
);
);
但它失败了
Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored
Column NPN1 Minus Target Formula Interrupted
Name Unresolved: col at row 1001 in access or evaluation of 'col' , col/*###*/
Formula evaluation errors have been ignored
通过强制对变量 col 进行赋值,代码将起作用
Names Default To Here( 1 );
dt =
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );
colNames = dt << get column names( continuous, string )<BR /><BR />For Each( {xcol}, colNames,
col = xcol;
theTarget = (Column( Eval( col ) ) << get property( "Spec Limits" ))["Target"];
Eval(
Eval Expr(
New Column( col || " Minus Target",
Numeric,
"Continuous",
Format( "Best", 10 ),
Formula( As Column( Eval( col ) ) - Expr( theTarget ) )
)
)
);
);
此外,一旦我运行代码
col = xcol;
我可以运行没有 xcol 分配的代码,原始代码将会运行。
这让我很困惑。有人能解释一下这背后的原因吗?
最有可能的是 JMP 的某个人必须提供正确答案。 @EvanMcCorkle ?
我不确定确切的原因,但其中一个原因是 col 不会在公式内进行评估(将 Eval() 更改为 Expr()以强制列名在 As Column() 内)(或者我认为您可以在创建公式后运行 << 运行公式,然后立即删除公式以确保它不会使用错误的列重新评估)。
如果 col 是您为每个容器值存储的变量,则它将不会存在于具有这些值的循环之外(将 xcol 设置为 col 可以稍微绕过这个问题)
Names Default To Here(1);
a = {"a", "b", "c"};
b = "z";
show(b);
For Each({b}, a,
show(b);
);
show(b);
/*
b = "z";
b = "a";
b = "b";
b = "c";
b = "z";
*/
我更喜欢使用这种方法(Expr(NameExpr(AsColumn())),因为这会使公式“更干净”(:name 而不是 AsColumn(“name”)。
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/semiconductor capability.jmp");
colNames = (dt << get column names(continuous, string))[1::10];
For Each({col}, colNames,
theTarget = (Column(Eval(col)) << get property("Spec Limits"))["Target"];
Eval(
Eval Expr(
New Column(col || " Minus Target",
Numeric,
"Continuous",
Format("Best", 10),
Formula(Expr(NameExpr(As Column(col))) - Expr(theTarget))
)
)
);
);
这很可能与......每个循环都具有函数局部范围的问题不同(这非常烦人......每个循环都不能正确地与函数的局部范围配合使用。我已将此问题报告给 JMP 支持)
Names Default To Here(1);
b = 2;
my_f = function({a}, {Default Local},
c = Transform Each({item}, a,
a * b
);
return(c);
);
my_f2 = function({a}, {Default Local},
b = b; // "Fix"
c = Transform Each({item}, a,
a * b
);
return(c);
);
Try(
show(my_f({1,2,3}));
,
show(exception_msg);
);
Try(
show(my_f2({1,2,3}));
,
show(exception_msg);
);