
受到启发有没有办法提取公式中使用的列?和伊恩@JMP评论。
dt = New Table( "Get Columns in Formula",
Add Rows( 20 ),
New Column( "H", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "W", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "D", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "V", Numeric, "Continuous", Format( "Best", 12 ), Formula( (:W * :H * :D) / 10 ) )
);
formula = dt:V << getformula();
// function to find leaves in formula that are columns in the table
findColumns = Function( {frm, cols},
{h = Head( frm ), n = N Arg( frm ), i, result = {}}, // local variables
If( n == 0, // head node has no children, it is a leaf
If( Contains( cols, Name Expr( h ) ), // is the leaf in the list of columns?
Insert Into( result, Name Expr( h ) ); // yes -> add to result
)
,
For( i = 1, i <= n, i++, // not a leaf, get children's results
Insert Into( result, Recurse( Arg( frm, i ), cols ) )
)
);
result// return the result
);
answer = findColumns( Name Expr( formula ), dt << GetColumnNames );
Show( formula, answer );
公式 = (:W * :H *
) / 10;
答案 = {:W, :H,
};
这查找列第 10-22 行定义的函数采用两个参数,弗罗姆,要检查的公式,以及列,要提取的有效列名称的列表。这头示例表达式的 Divide(...) 它有两个子表达式(答案来自精氨酸)。 if 语句看到有孩子并使用递归处理每个孩子的函数。多子有三个孩子。:W 子级没有子级,并且数据表中的列列表包含 :W,因此 :W 是插入到一个列表和返回的列表,其中该列表被插入到另一个列表中。然后乘法子发送第二个精氨酸递归并附加 :H。最终10 (除法的第二个孩子)被发送到递归,但不在列列表中,因此不会出现在答案中。
递归每次都需要付出一些努力。但它制作的程序看起来很简单。