Trova ed estrai un argomento di espressione con una data testa di espressioneVoglio scrivere uno script efficiente che accetti come input un'espressione JSL e:
- Trova un argomento in quell'espressione che abbia una testa di espressione specifica
- Sostituisci quell'argomento con una versione modificata di quell'argomento
Esempio:
Ho questa espressione
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 />);Voglio trovare l'argomento "LSMeans Contrast(...)" nascosto nell'espressione ed estrarre tale espressione in una nuova espressione, chiamiamola espressione_estratta .
Quindi in questo caso voglio
extracted_expression = Expr(LS Means Contrast([1 0 -1]))
Voglio quindi ottenere l'argomento per quell'espressione, modificarlo e quindi modificare extracted_expression e sostituire l'espressione dell'argomento modificata con l'espressione estratta originale. Ad esempio:
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));
L'espressione risultante sarebbe
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] )}}}
)
)
Il punto in cui mi blocco è il passaggio 1, ovvero come attraversare gli argomenti dell'espressione originale per trovare l'argomento che ha un'espressione con l'intestazione specificata (in questo esempio, l'intestazione dell'argomento è LS significa contrasto ).
Avete suggerimenti/idee su come farlo?
Automazione e scriptingRe: Trova ed estrai un argomento di espressione con una data espressione headNota che questo non è il vero problema su cui sto lavorando: l'espressione fornita sopra è solo un esempio che ha un argomento di espressione nascosto in profondità in un'espressione.
Re: Trova ed estrai un argomento di espressione con una data testa di espressioneEstrai Expr() fa la magia : )

Re: Trova ed estrai un argomento di espressione con una data espressione headExtract Expr forse è quello che stai cercando?
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);
Re: Trova ed estrai un argomento di espressione con una data espressione headEcco alcuni dettagli/insidie di extract expr():
Estrai Expr
Re: Trova ed estrai un argomento di espressione con una data espressione headimportante sapere:
estrai expr() non è avido, si fermerà dopo aver trovato la prima corrispondenza.
D'altro canto, Sostituire() è avido.
La sostituzione in 2 passaggi nel codice di @jthi è essenziale!
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]);In questo caso il problema è ovvio, ma ci sono altri casi in cui è più difficile controllare quale espressione viene sostituita.
desideri correlati:
Sostituisci prima occorrenza
:folded_hands: Indicizzazione delle espressioni: accesso in lettura e scrittura
subst() - sostituisci() 2.0
Re: Trova ed estrai un argomento di espressione con una data espressione headGrazie @jthi e @hogi . Non avevo familiarità con Extract Expr() e l'espressione Wild(). Funziona bene per quello su cui sto effettivamente lavorando. Molto apprezzato.