迭代列我正在编写一个脚本来遍历一列列表,但遇到了一个问题。我需要能够以“: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 - 只需删除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 ":"