<?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 How to make recursive function with associative array as parameter in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539252#M76024</link>
    <description>&lt;P&gt;I'm trying to parse a nested associative array in JMP 16 (extracted from a JSON). The basic structure of the JSON is as follows:&lt;/P&gt;&lt;PRE&gt;{&lt;BR /&gt;  "groupOp": "&amp;amp;",
  "rules": [
    {"column": "col1", "op": "==", "value": 1},
    {&lt;BR /&gt;      "groupOp": "|",&lt;BR /&gt;      "rules": [&lt;BR /&gt;        {"column": "col2", "op": "==", "value": 2}&lt;BR /&gt;        {"column": "col3", "op": "==", "value": 3}&lt;BR /&gt;      ]&lt;BR /&gt;    }&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;This would allow structured storage of the nested condition "col1 == 1 &amp;amp; (col2 == 2 | col3 == 3)", and others of arbitrary depth.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote a recursive function to parse this structure, using the contains function to distinguish the base case from the recursive case. However, I'm running into a stack overflow error. Upon debugging I realized that while "rule" had the correct value, "obj" stayed the same on each recursive call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;parseFilter = Function( {obj},
	If (obj &amp;lt;&amp;lt; contains("groupOp") &amp;amp; obj &amp;lt;&amp;lt; contains("rules"),
		str = "(";
		For Each({rule}, obj["rules"],
			show(rule);
			Concat(str, Recurse(rule) || " " || groupOp || " ");
		);
		Concat(str, ")");
		Return(str);
	, // else
		Return(":" || obj["column"] || " " || obj["op"] || " " || data );
	);
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Is this to do with the For Each function, having an associative array as a parameter, or something else?&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 17:08:55 GMT</pubDate>
    <dc:creator>jconte</dc:creator>
    <dc:date>2023-06-09T17:08:55Z</dc:date>
    <item>
      <title>How to make recursive function with associative array as parameter</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539252#M76024</link>
      <description>&lt;P&gt;I'm trying to parse a nested associative array in JMP 16 (extracted from a JSON). The basic structure of the JSON is as follows:&lt;/P&gt;&lt;PRE&gt;{&lt;BR /&gt;  "groupOp": "&amp;amp;",
  "rules": [
    {"column": "col1", "op": "==", "value": 1},
    {&lt;BR /&gt;      "groupOp": "|",&lt;BR /&gt;      "rules": [&lt;BR /&gt;        {"column": "col2", "op": "==", "value": 2}&lt;BR /&gt;        {"column": "col3", "op": "==", "value": 3}&lt;BR /&gt;      ]&lt;BR /&gt;    }&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;This would allow structured storage of the nested condition "col1 == 1 &amp;amp; (col2 == 2 | col3 == 3)", and others of arbitrary depth.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote a recursive function to parse this structure, using the contains function to distinguish the base case from the recursive case. However, I'm running into a stack overflow error. Upon debugging I realized that while "rule" had the correct value, "obj" stayed the same on each recursive call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;parseFilter = Function( {obj},
	If (obj &amp;lt;&amp;lt; contains("groupOp") &amp;amp; obj &amp;lt;&amp;lt; contains("rules"),
		str = "(";
		For Each({rule}, obj["rules"],
			show(rule);
			Concat(str, Recurse(rule) || " " || groupOp || " ");
		);
		Concat(str, ")");
		Return(str);
	, // else
		Return(":" || obj["column"] || " " || obj["op"] || " " || data );
	);
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Is this to do with the For Each function, having an associative array as a parameter, or something else?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 17:08:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539252#M76024</guid>
      <dc:creator>jconte</dc:creator>
      <dc:date>2023-06-09T17:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to make recursive function with associative array as parameter</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539273#M76026</link>
      <description>&lt;P&gt;Without studying too deep, the first change you probably need is to make &lt;EM&gt;str&lt;/EM&gt; be local. Change&lt;/P&gt;
&lt;LI-CODE lang="jsl"&gt;parseFilter = Function( {obj},&lt;/LI-CODE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;parseFilter = Function( {obj},{str},&lt;/LI-CODE&gt;
&lt;P&gt;It looks like another parameter list, but it is actually a list of variables that are local to the function, and to each recursive call of the function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2022 21:50:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539273#M76026</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2022-08-31T21:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to make recursive function with associative array as parameter</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539278#M76027</link>
      <description>&lt;P&gt;Thanks for the tip. Still running into the same issue though.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2022 21:55:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539278#M76027</guid>
      <dc:creator>jconte</dc:creator>
      <dc:date>2022-08-31T21:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to make recursive function with associative array as parameter</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539318#M76028</link>
      <description>&lt;P&gt;I think this might be what you are after. Untested code follows...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;json =
"\[
{
  "groupOp": "&amp;amp;",
  "rules": [
    {"column": "col1", "op": "==", "value": 1},
    {
      "groupOp": "|",
      "rules": [
        {"column": "col2", "op": "==", "value": 2},
        {"column": "col3", "op": "==", "value": 3}
      ]
    }
    ]
}
	
]\";

parseFilter = Function( {obj},
	{str, first},
	If( obj &amp;lt;&amp;lt; Contains( "groupOp" ) &amp;amp; obj &amp;lt;&amp;lt; Contains( "rules" ),
		str = "(";
		first = 1;
		For Each( {rule}, obj["rules"],
			If( first,
				first = 0;
			,// else
				str ||= (" " || obj["groupOp"] || " ")
			);
			str ||= Recurse( rule );
		);
		str ||= ")";
		Return( str );
	, // else
		Return( ":" || obj["column"] || " " || obj["op"] || " " || Char( obj["value"] ) )
	)
);

Write( parsefilter( Parse JSON( json ) ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;(:col1 == 1 &amp;amp; (:col2 == 2 | :col3 == 3))&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 01:03:41 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-make-recursive-function-with-associative-array-as/m-p/539318#M76028</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2022-09-01T01:03:41Z</dc:date>
    </item>
  </channel>
</rss>

