<?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 Re: Parse a column formula and swap out parameters for column references in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86094#M38425</link>
    <description>&lt;P&gt;Hi Ian, thanks!&lt;/P&gt;&lt;P&gt;The problem is that your solution keeps evaluating the whole equation and when it finds the letter a (one of the parameters) contained in a word somewhere it will also replace that with the replacement column. So I need to evaluate each argument only once&amp;nbsp;and only if the argument is by itself and not contained in a&amp;nbsp;column name. I'll rework my example to include one that fails.&lt;/P&gt;</description>
    <pubDate>Tue, 18 Dec 2018 17:21:34 GMT</pubDate>
    <dc:creator>pauldeen</dc:creator>
    <dc:date>2018-12-18T17:21:34Z</dc:date>
    <item>
      <title>Parse a column formula and swap out parameters for column references</title>
      <link>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86018#M38413</link>
      <description>&lt;P&gt;I'm writing a script that will allow users to select one of the fit models in Non-linear platform. Save the parameterized version out to the data table and then&amp;nbsp;take the parameters&amp;nbsp;and replace them with column references. So how to&amp;nbsp;detect the parameters in the equation, change them and insert the whole thing back into the column as a formula.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In general I want to parse all arguments of a formula, detect&amp;nbsp;when they are parameters, swap them out and reassemble the formula. I do not know upfront how many nested levels of arguments there are or how many parameters there will be.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example script of what I could do, with some comments and challenges:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dttest = New Table( "Test table",
	Add Rows( 5 ),
	Compress File When Saved( 1 ),
	New Column( "Model",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			Parameter( {a = 1, b = 2, c = 3}, a + b * :Tecta + c * :Tecta ^ 2 )
		)
	),
	New Column( "Tecta",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [1, 2, 3, 4, 5] )
	),
	New Column( "New a col ctttc",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [1, 1, 1, 1, 1] )
	),
	New Column( "New b col ctttc",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2, 2, 2, 2, 2] )
	),
	New Column( "New c col atttc",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [3, 3, 3, 3, 3] )
	)
);

FormulaScript = column("Model") &amp;lt;&amp;lt; get formula;
Parameters = arg(FormulaScript, 1);
Equation = arg(FormulaScript, 2);
NewParameters = remove(dttest &amp;lt;&amp;lt; Get Column Names(string),1,n cols(dttest)-n items(Parameters));

StrictParameters = {};
for(i=1, i&amp;lt;=n items(Parameters), i++,
	insert into(StrictParameters,arg(Parameters[i],1));
);
	
ParameterSwap = Associative Array(StrictParameters, NewParameters);

//Step through all levels of equation to swap occurences of parameters with the corresponding index of NewParameters.
//Must be robust for no space before or after the parameters name.
//Must be robust for parameters being named "a" and be part of the name of a column.
//I tried using substitute but that will replace all instances of "a" with a column references and that does not go well.
//Tried using arg() but then you will have to traverse unknown levels of arguments in the equations and piece by piece reassemble the whole equation.

//finally rebuild the column formula with the new equation where the parameters are replaced bu col references.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Dec 2018 22:16:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86018#M38413</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2018-12-17T22:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Parse a column formula and swap out parameters for column references</title>
      <link>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86073#M38421</link>
      <description>&lt;P&gt;Not sure if this is quite what you want, or indeed, if it's general enough if it is. But adding to your code, Paul:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;for(p=1, p&amp;lt;=NItems(ParameterSwap), p++,
	thisKey = (ParameterSwap &amp;lt;&amp;lt; getKeys)[p];
	thisValue = (ParameterSwap &amp;lt;&amp;lt; getValues)[p];
	Equation = Substitute(NameExpr(Equation), EvalExpr(thisKey), EvalExpr(AsColumn(dttest, thisValue)));
	Print(Equation);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 15:00:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86073#M38421</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2018-12-18T15:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Parse a column formula and swap out parameters for column references</title>
      <link>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86094#M38425</link>
      <description>&lt;P&gt;Hi Ian, thanks!&lt;/P&gt;&lt;P&gt;The problem is that your solution keeps evaluating the whole equation and when it finds the letter a (one of the parameters) contained in a word somewhere it will also replace that with the replacement column. So I need to evaluate each argument only once&amp;nbsp;and only if the argument is by itself and not contained in a&amp;nbsp;column name. I'll rework my example to include one that fails.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 17:21:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Parse-a-column-formula-and-swap-out-parameters-for-column/m-p/86094#M38425</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2018-12-18T17:21:34Z</dc:date>
    </item>
  </channel>
</rss>

