查找并提取具有给定表达式头的表达式参数我想编写一个高效的脚本,将 JSL 表达式作为输入并:
- 在该表达式中查找具有特定表达式头的参数
- 用该论点的修改版本替换该论点
例子:
我有这种表情
my_expr=Expr(<BR />Fit Model(
Y( :y ),
Effects( :Drug, :x ),
Personality( "Standard Least Squares" ),
Emphasis( "Effect Leverage" ),
Run(
:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
{:Drug << {LSMeans Contrast( [1 0 -1] )}}}
)
)<BR />);我想找到隐藏在表达式中的“LSMeans Contrast(...)”参数,并将该表达式提取到一个新的表达式中,我们将其称为提取的_表达式 。
所以在这种情况下我想要
extracted_expression = Expr(LS Means Contrast([1 0 -1]))
然后我想要获取该表达式的参数,修改它,然后修改extracted_expression,并用修改后的参数表达式替换原始提取的表达式。例如:
new_expression=Substitute(Name Expr(extracted_expression), arg(extracted_expression,1), [-1 0 .5]);
Substitute(Name Expr(my_expr), Name Expr(extracted_expression), Name Expr(new_expression));
得到的表达式为
Fit Model(
Y( :y ),
Effects( :Drug, :x ),
Personality( "Standard Least Squares" ),
Emphasis( "Effect Leverage" ),
Run(
:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
{:Drug << {LS Means Contrast( [-1 0 0.5] )}}}
)
)
我遇到困难的地方是第 1 步,如何遍历原始表达式的参数以找到具有指定标题的表达式的参数(在此示例中,参数的头部是LS 均值对比度 )。
关于如何做到这一点有什么建议/想法吗?
自动化和脚本回复:查找并提取具有给定表达式头的表达式参数请注意,这不是我正在处理的真正问题,上面提供的表达式只是一个在表达式深处埋藏表达式参数的示例。
回复:查找并提取具有给定表达式头的表达式参数提取 Expr()创造了奇迹:)

回复:查找并提取具有给定表达式头的表达式参数提取 Expr 也许就是您正在寻找的?
Names Default To Here(1);
my_expr=Expr(Fit Model(
Y( :y ),
Effects( :Drug, :x ),
Personality( "Standard Least Squares" ),
Emphasis( "Effect Leverage" ),
Run(
:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
{:Drug << {LSMeans Contrast( [1 0 -1] )}}}
)
));
myhead = Extract Expr(my_expr, LSMeans Contrast(Wild()));
myarg = Arg(part, 1);
//show(myhead, myarg);
new_expr = Substitute(Name Expr(myhead), myarg, [1 0 1]);
my_expr2 = Substitute(Name Expr(my_expr), Name Expr(myhead), Name Expr(new_expr));
Show(my_expr, my_expr2);
回复:查找并提取具有给定表达式头的表达式参数以下是extract expr() 的一些细节/缺陷:
提取表达式
回复:查找并提取具有给定表达式头的表达式参数重要提示:
提取 expr()不是贪婪的,它会在找到第一个匹配后停止。
另一方面, 代替()是贪婪的。
@jthi的代码中的两步替换至关重要!
my_expr=Expr(Fit Model(
{:Drug << {LSMeans Contrast( [1 0 -1] )}}
);
[1 0 -1] // another time the same Arg, lives in danger
);
part= Extract Expr(my_expr, LSMeans Contrast(Wild()));
myarg = Arg(part, 1); //part instead of myhead
new_expr = Substitute(Name Expr(myhead), myarg, [1 0 1]);
my_expr2 = Substitute(Name Expr(my_expr), Name Expr(myhead), Name Expr(new_expr));<BR /><BR />// won't work<BR />// my_expr3 = Substitute(Name Expr(my_expr), Name Expr(myarg), [1 0 1]);这里的问题很明显——但在其他情况下,控制哪个表达式被替换就更加困难了。
相关愿望:
替换第一次出现
:folded_hands: 表达式索引:读写访问
subst() - substitute() 2.0
回复:查找并提取具有给定表达式头的表达式参数感谢@jthi和@hogi 。我不太熟悉 Extract Expr() 和 Wild() 表达式。对于我实际正在做的工作来说,效果很好。非常感谢。