<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Find and extract an expression argument with a given expression head in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836727#M101356</link>
    <description>&lt;P&gt;I want to write an efficient script that will take as an input a JSL expression and:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Find an argument in that expression that has a specific expression head&lt;/LI&gt;&lt;LI&gt;Substitute that argument with a modified version of that argument&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this expression&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;my_expr=Expr(&lt;BR /&gt;Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y &amp;lt;&amp;lt; {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 &amp;lt;&amp;lt; {LSMeans Contrast( [1 0 -1] )}}}
	)
)&lt;BR /&gt;);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to find &amp;nbsp;the "LSMeans Contrast(...)" argument that is buried in the expression and extract that expression in to a new expression, let's call that &lt;EM&gt;&lt;STRONG&gt;extracted_expression&lt;/STRONG&gt;&lt;/EM&gt;. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in this case I want&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;extracted_expression = Expr(LS Means Contrast([1 0 -1]))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then want to get the argument to that expression, modify it, and then modify the extracted_expression, and substitute the modified argument expression for the original extracted expression. . For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The resulting expression would be&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y &amp;lt;&amp;lt; {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 &amp;lt;&amp;lt; {LS Means Contrast( [-1 0 0.5] )}}}
	)
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where I am getting stuck is step 1, how to traverse the arguments of the original expression to find the argument that has an an expression with the specified header (in this example, the head of the argument is &lt;EM&gt;&lt;STRONG&gt;LS Means Contrast&lt;/STRONG&gt;&lt;/EM&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions/ideas on how to do this? &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 06 Feb 2025 16:47:42 GMT</pubDate>
    <dc:creator>MathStatChem</dc:creator>
    <dc:date>2025-02-06T16:47:42Z</dc:date>
    <item>
      <title>Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836727#M101356</link>
      <description>&lt;P&gt;I want to write an efficient script that will take as an input a JSL expression and:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Find an argument in that expression that has a specific expression head&lt;/LI&gt;&lt;LI&gt;Substitute that argument with a modified version of that argument&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this expression&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;my_expr=Expr(&lt;BR /&gt;Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y &amp;lt;&amp;lt; {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 &amp;lt;&amp;lt; {LSMeans Contrast( [1 0 -1] )}}}
	)
)&lt;BR /&gt;);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to find &amp;nbsp;the "LSMeans Contrast(...)" argument that is buried in the expression and extract that expression in to a new expression, let's call that &lt;EM&gt;&lt;STRONG&gt;extracted_expression&lt;/STRONG&gt;&lt;/EM&gt;. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in this case I want&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;extracted_expression = Expr(LS Means Contrast([1 0 -1]))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then want to get the argument to that expression, modify it, and then modify the extracted_expression, and substitute the modified argument expression for the original extracted expression. . For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The resulting expression would be&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y &amp;lt;&amp;lt; {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 &amp;lt;&amp;lt; {LS Means Contrast( [-1 0 0.5] )}}}
	)
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where I am getting stuck is step 1, how to traverse the arguments of the original expression to find the argument that has an an expression with the specified header (in this example, the head of the argument is &lt;EM&gt;&lt;STRONG&gt;LS Means Contrast&lt;/STRONG&gt;&lt;/EM&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions/ideas on how to do this? &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 16:47:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836727#M101356</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-02-06T16:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836740#M101359</link>
      <description>&lt;P&gt;Note, this isn't the real problem I'm working on, the expression provided above is just an example that has an expression argument buried deep in an expression. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 16:49:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836740#M101359</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-02-06T16:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836741#M101360</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Extract Expr()&lt;/FONT&gt;does the magic&amp;nbsp;&amp;nbsp;: )&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_0-1738860892387.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/72557i1244FBE0BFD34FA1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_0-1738860892387.png" alt="hogi_0-1738860892387.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 16:54:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836741#M101360</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-02-06T16:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836742#M101361</link>
      <description>&lt;P&gt;Extract Expr maybe what you are looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);


my_expr=Expr(Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y &amp;lt;&amp;lt; {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 &amp;lt;&amp;lt; {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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 16:52:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836742#M101361</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-02-06T16:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836747#M101362</link>
      <description>&lt;P&gt;Here are some details /pitfalls of&lt;FONT face="courier new,courier"&gt; extract expr():&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;LI-MESSAGE title="Extract Expr" uid="777106" url="https://community.jmp.com/t5/Discussions/Extract-Expr/m-p/777106#U777106" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 17:09:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836747#M101362</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-02-06T17:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836748#M101363</link>
      <description>&lt;P&gt;important to know:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;extract expr()&lt;/FONT&gt; is &lt;U&gt;not&lt;/U&gt; greedy , it will stop after finding the first match.&lt;BR /&gt;On the other hand, &lt;FONT face="courier new,courier"&gt;Substitute()&lt;/FONT&gt; IS greedy.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The 2-step &lt;FONT face="courier new,courier"&gt;Substitute &lt;/FONT&gt;in&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;'s code is essential!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;my_expr=Expr(Fit Model(
		{:Drug &amp;lt;&amp;lt; {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));&lt;BR /&gt;&lt;BR /&gt;//&amp;nbsp;won't&amp;nbsp;work&lt;BR /&gt;//&amp;nbsp;my_expr3 = Substitute(Name Expr(my_expr),&amp;nbsp;Name Expr(myarg), [1 0 1]);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here, the issue is obvious - but there are other cases where it is more difficult to control which expression gets substituted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;related wishes:&lt;BR /&gt;&lt;LI-MESSAGE title="Substitute First Occurrence" uid="574766" url="https://community.jmp.com/t5/JMP-Wish-List/Substitute-First-Occurrence/m-p/574766#U574766" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="🙏 Expression Indexing: read and write access" uid="697359" url="https://community.jmp.com/t5/JMP-Wish-List/Expression-Indexing-read-and-write-access/m-p/697359#U697359" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="subst() - substitute() 2.0" uid="776918" url="https://community.jmp.com/t5/JMP-Wish-List/subst-substitute-2-0/m-p/776918#U776918" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 17:20:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836748#M101363</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-02-06T17:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find and extract an expression argument with a given expression head</title>
      <link>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836808#M101367</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/26800"&gt;@hogi&lt;/a&gt;&amp;nbsp;. &amp;nbsp;I wasn't familiar with Extract Expr() and the Wild() expression. &amp;nbsp;Works nicely for what I am actually working on. &amp;nbsp;Much appreciated. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 19:13:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Find-and-extract-an-expression-argument-with-a-given-expression/m-p/836808#M101367</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-02-06T19:13:50Z</dc:date>
    </item>
  </channel>
</rss>

