迭代列我正在編寫一個腳本來迭代列列表,但遇到了障礙。我需要能夠以 ":Technology Default" 的形式輸出列引用,但我查看過的每種方法都會輸出 "Column("Technology Default")" 。我得到的是「列物件參考」時,是「列引用」嗎?
自動化和腳本編寫回覆:迭代列你想做什麼?您可以使用 AsColumn() 和 Column() (有時是 Eval())(對於該格式,As Column() 通常應該可以工作,但您可能需要將其與表達式一起使用)。
編輯:
幾個連結:
例如,如果您希望在公式中獲得該格式,EvalExpr + Name Expr + As Column 是我通常開始的方法
Names Default To Here(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
colname = "height";
Eval(EvalExpr(
dt << New Column(Eval Insert("^colname^*^colname^"), Numeric, Continuous, Formula(
Expr(Name Expr(AsColumn(dt, colname))) * Expr(Name Expr(AsColumn(dt, colname)))
));
));
回覆:迭代列我的最終目標是擁有可以在新列的公式中使用的東西。公式僅在指定為“:Column”時才識別列,就像看起來一樣。無論如何,借助我在此連結中找到的資訊。
已解決:如何替換字串中的雙引號 - JMP 用戶社區
我想出了一個蠻力方法來完成目標。它並不優雅,但似乎很有效。仍然願意接受有關如何更正式地做到這一點的建議。
再見
扳手頭
dt = Current Data Table();
lst = dt << Get Column Names(string);
For(i=1,i<=NItems(lst),i++,
colName = lst[i];
col = Column(colName);
Colstring = (":"||colName);
Colref = Parse( Eval Insert(Colstring));
Show(Colref););回覆:迭代列我通常不會像JMP 中那樣搞亂列引用,因為管理它們太煩人了-> 我使用列名稱的字串,並根據需要將它們與Column()、As Column()、Eval() 或NameExpr 一起使用(AsColumn()) (如果我出於某種原因需要使用列引用 (Column("something")) ,當我需要使用它們時,我將使用 << Get Name 將它們轉換為字串)。
Names Default To Here(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
col1 = "height";
col2 = "weight";
<BR />// Good way but can be (sometimes) difficult to read
Eval(EvalExpr(
dt << New Column("mycol1", Numeric, Continuous, Formula(
Expr(Name Expr(AsColumn(dt, col1))) * Expr(Name Expr(AsColumn(dt, col2)))
))
));
<BR />// might be easier to read
Eval(Substitute(
Expr(dt << New Column("mycol2", Numeric, Continuous, Formula(
_firstcol_ * _secondcol_
))),
Expr(_firstcol_), Name Expr(AsColumn(dt, col1)),
Expr(_secondcol_), Name Expr(AsColumn(dt, col2))
));
// "reusable"
ref1 = Name Expr(AsColumn(dt, col1)); // this will return :height BUT you will generally still need Name Expr() to use it
ref2 = Name Expr(AsColumn(dt, col2));
// wont work, because ref1 will get evaluated for "current" row
Eval(EvalExpr(
dt << New Column("mycol3", Numeric, Continuous, Formula(
Expr(ref1) * Expr(ref2)
))
));
// will work
Eval(EvalExpr(
dt << New Column("mycol3", Numeric, Continuous, Formula(
Expr(Name Expr(ref1)) * Expr(Name Expr(ref2))
))
));
如果您嘗試僅使用
dt << New Column("mycol4", Numeric, Continuous, Formula(
ref1 * ref2
));
你最終會得到像 ref1*ref2 這樣的公式,因為這些公式不會被評估為公式

更多連結:
回覆:迭代列我可能找錯了樹,但您遇到的問題似乎是您在公式中使用了列引用,而您需要做的是在創建公式時解析這些引用。 Jarmo 的範例給出了這樣做的範例,但可能有點令人困惑。下面嘗試透過一個非常簡單的範例來解釋問題和解決方案
// Here is a sample script, that creates a new data table
// with 2 columns, and what we want to do, is to simply
// put the value 1 into column c1, and a 2 into column c2
// In the first example, it is done by simply running a
// for() loop and taking the index variable, "i" and using it in
// the formula and squaring it's value.
Names Default To Here( 1 );
dt = New Table( "Example" );
dt<< add rows(1);
For( i = 1, i <= 2, i++,
dt << New Column( "c" || Char( i ),
formula( i^2 )
)
);
// When each column was created, it used the value of i at that
// time within the formula. Thus, for c1, the value of i was 1,
// and for c2 it was 4.
// So now, if we rerun the formulas, it will take the value of
// i at the time of rerunning each formula.
wait(5);
dt<<RERUN formulas=""></RERUN>
<P>我希望這有幫助</P>回覆:迭代列而不是“蠻力”
Colstring = (":"||colName);
Colref = Parse( Eval Insert(Colstring));更優雅的方式是:
Colref= Name Expr (As Column(colName))
-> 請參閱@jthi的回覆。
它已經幫助了許多其他用戶: JMP 中的表達式處理:Tipps 和 Trapdoors
順便一提:
在您的範例中,您可以透過「取得列名稱」直接取得Colref( s) - 只需刪除STRING參數即可。
他們前面沒有“:”,但這只是化妝品:)
dt = Current Data Table();
lst = dt << Get Column Names();
For( i = 1, i <= N Items( lst ), i++,
Show( lst[i] );
Show( lst[i][5] );
);
回覆:迭代列如果您喜歡以“:”開頭的列名,您可以使用一個技巧:
問他們每個人的名字。
columnNames = current data table() << Get Column Names;
for (i=1, i<= Nitems(columnNames),i++,columnNames[i]<< get name() );
show(columnNames) // -> column names with leading ":"