输出是:
包含(collist,col)= 0;
包含(collist,作为名称(colname))= 1;
回复:为什么这些引用列的方式会给出不同的结果?此表达式不存储列引用:
col = AsName("abc");
所以 Contains() 函数返回 0(假)。如果要存储列引用,则应使用 Column( "name" )。
回复:为什么这些引用列的方式会给出不同的结果?前方的虚拟兔子洞。马克的答案可能就是你所需要的。
这是一个令人着迷的问题。看起来你应该得到相同的答案,但是......
与 C 和许多其他语言不同,函数的参数在调用函数之前不会被求值。相反,函数决定如何处理其参数。并且,JSL 有一个称为表达式的变量类型,与 AsName 返回的值密切相关。当您使用表达式参数调用函数时,该函数可以选择如何处理该参数。它可能会评估表达式; a+2 需要查找变量 a 并添加 2。否则它可能会操纵表达式; head(a+2) 返回add 。在您的示例中,表达式为abc ,查找它意味着从数据表中的行获取其值。
(这很难,我花了一个小时试图使评论正确且清晰。我认为它们是正确的。)
dt = New Table( "test", New Column( "abc" ), New Column( "def" ) );
collist = {:abc, :def, .}; // <<< added a missing value in position 3
col = As Name( "abc" );// the Right Hand Side evaluates to the expression abc,
// and the expression is assigned to col. The 2nd argument to contains() is
// a name expression, and contains() evaluates that argument. It
// evaluates to missing because there is no subscript to pick a row.
Show( Name Expr( col ), Type( col ), Contains( collist, col ) ); // looks for a missing value
// Name Expr(col) = abc;
// Type(col) = "Number";
// Contains(collist, col) = 3;
colname = "abc";
// contains() evaluates the 2nd argument to a name, but does not
// do another evaluation of the name, so it locates the name
// in the list of names
Show( Type( As Name( colname ) ), Contains( collist, As Name( colname ) ) );
// Type(As Name(colname)) = "Name";
// Contains(collist, As Name(colname)) = 1;
最后,
Show( Name Expr( col ), Type( col ), Contains( collist, Name Expr(col) ) );
做你想做的事; contains() 的第二个参数具有 nameexpr() 包装器,该包装器导致 contains() 将其计算为表达式的名称abc ,而不是值。
编辑:这绝非易事! @hogi指出 head(a+2) 示例(上面)在《Head 评估其参数吗?》中是错误的。 ;我认为答案是 head() 查看参数以查看它是否是保存表达式的变量,如果不是,则对其求值以查看它是否会生成表达式。
为什么这些引用列的方式会产生不同的结果?我正在努力理解 JSL 如何处理列引用,并因此不断在我的脚本中遇到错误。
为什么以下表达式给出不同的结果?
将列引用动态传递给函数的最佳方法是什么?
dt = New Table("test",
New Column("abc"),
New Column("def")
);
collist = {:abc, :def};
col = AsName("abc");
colname = "abc";
Show(Contains(collist, col));
Show(Contains(collist, AsName(colname)));输出是:
Contains(collist, col) = 0;
Contains(collist, As Name(colname)) = 1;
自动化和脚本编写回复:为什么这些引用列的方式会给出不同的结果?@Craige_Hales 。
谢谢你的这篇文章 - 我会将它添加到我的珍宝清单中:)
它给出了一些提示为什么名称 Expr(作为列(colname))
[例如通过@jthi在回复:需要有关表达式、列引用、公式和列重命名的帮助】 太有用了!
我想说:我想我现在真的明白了——但下面的一些结果仍然让我感到惊讶。
我仍在寻找全面的文档 - 例如如何找到如何函数处理它的参数。
当搜索通过包含,为什么 Jmp 知道列(“abc”)=:abc?
dt = New Table( "test", New Column( "abc" ), New Column( "def" ) );
collist = {:abc, :def, .}; // <<< added a missing value in position 3
row()=0; //to be sure that the row index is really 0 :)
colname = "abc";
col = As Name( colname); // abc
col2 = :name("abc"); // .
col3 = As Column(colname); // .
col4 = Name Expr(As Column(colname)); // :abc
col5 = Column(colname); // Column("abc")
col6 = Name Expr(Column(colname)); // Column( colname )
col; // .
col2; // .
col3; // .
col4; // .
col5; // Column( "abc" )
col6; // Column( "abc" )
Show( Name Expr( col ), Type( col ), Contains( collist, col ) ); // :abc, Number, 3
Show( Name Expr( col2 ), Type( col2 ), Contains( collist, col2 ) ); // . , Number, 3
Show( Name Expr( col3 ), Type( col3 ), Contains( collist, col3 ) ); // ., Number, 3
Show( Name Expr( col4 ), Type( col4 ), Contains( collist, col4 ) ); // :abc, Number, 3
Show( Name Expr( col5 ), Type( col5 ), Contains( collist, col5 ) ); // Column("abc"), Column, 1 (!)
Show( Name Expr( col6 ), Type( col6 ), Contains( collist, col6 ) ); // Column(colname), Column, 1 (!!)
Show( Contains( collist, Name Expr(col) ) ); // 1
Show( Contains( collist, Name Expr(col2) ) ); // 3
Show( Contains( collist, Name Expr(col3) ) ); // 3
Show( Contains( collist, Name Expr(col4) ) ); // 1
Show( Contains( collist, Name Expr(col5) ) ); // 1
Show( Contains( collist, Name Expr(col6) ) ); // 0