在解決迭代創建包含公式的列時,我遇到了需要以一種我不明白為什麼的方式建立解決方案的需求。我最初的解決方案是
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() 內)(或者我認為你可以run < < 建立公式後執行公式,然後立即刪除公式以確保不會使用不正確的列重新評估)。
如果 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);
);