<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Expression Rule</title><link>https://community.appian.com/discussions/f/rules/21640/expression-rule</link><description>Hi, 
 I have a below output from my expression 
 My requirement is if &amp;quot;expr&amp;quot; of every Dictionary contains &amp;quot;rowcode&amp;quot; that row code needs to be replaced by &amp;quot;expr&amp;quot; of that particular rowcode (for eg: &amp;quot;(FRML_88_1+FRML_88_2)&amp;quot; this expression contains rowcode</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84657?ContentTypeID=1</link><pubDate>Tue, 17 Aug 2021 04:16:02 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:7c54151c-9e89-48a2-9c82-53f17d4ee553</guid><dc:creator>NR</dc:creator><description>&lt;p&gt;&lt;span&gt;Thanks&amp;nbsp; for your reply, it&amp;#39;s&amp;nbsp; really a clean code&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84656?ContentTypeID=1</link><pubDate>Tue, 17 Aug 2021 04:11:46 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a09cb09d-73af-486d-8e5c-604baff61077</guid><dc:creator>NR</dc:creator><description>&lt;p&gt;&lt;span&gt;Thanks Chris for your response, it&amp;#39;s working as expected&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84632?ContentTypeID=1</link><pubDate>Mon, 16 Aug 2021 18:18:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:165e6498-56bf-428d-8591-93e060929a97</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Nice code, much cleaner!&amp;nbsp; Would love to runs some tests cases with it but alas, we are only on 21.1 (no a!update() yet)!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84629?ContentTypeID=1</link><pubDate>Mon, 16 Aug 2021 17:50:49 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:fd7ffae9-e79e-47a5-9e90-0688d73d8e72</guid><dc:creator>Peter Lewis</dc:creator><description>&lt;p&gt;This is a fun problem! I think you could simplify it by assuming that the operators don&amp;#39;t need to be specified individually, since each of the sub-expressions are valid on their own. If you do, then you can do a more straightforward substitute(). Also, this is one of the few examples where the reduce() function actually makes this easier! The important thing here is that the result of the previous must be used for the next loop (since you&amp;#39;d need that to&amp;nbsp;use the result of FRML_88_3 in the last item). Here&amp;#39;s what I did:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main Rule&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!data: {
    a!map(
      expr: &amp;quot;(123/12)&amp;quot;,
      rowcode: &amp;quot;FRML_88_1&amp;quot;
    ),
    a!map(
      expr: &amp;quot;(1284*864)&amp;quot;,
      rowcode: &amp;quot;FRML_88_2&amp;quot;
    ),
    a!map(
      expr: &amp;quot;(FRML_88_1+FRML_88_2)&amp;quot;,
      rowcode: &amp;quot;FRML_88_3&amp;quot;
    ),
    a!map(
      expr: &amp;quot;(9090+122)&amp;quot;,
      rowcode: &amp;quot;FRML_88_4&amp;quot;
    ),
    a!map(
      expr: &amp;quot;(FRML_88_3*FRML_88_4)&amp;quot;,
      rowcode: &amp;quot;FRML_88_5&amp;quot;
    )
  },
  reduce(
    rule!PDL_RecursiveSupportingRule,
    local!data,
    enumerate(length(local!data)) + 1,
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Supporting Rule&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!forEach(
  items: ri!data,
  expression: if(
    search(
      index(ri!data.rowcode, ri!index, {}),
      fv!item.expr
    ) &amp;gt; 0,
    a!update(
      data: fv!item,
      index: &amp;quot;expr&amp;quot;,
      value: substitute(
        fv!item.expr,
        index(ri!data.rowcode, ri!index, {}),
        index(ri!data.expr, ri!index, {}),     
      ),
    ),
    fv!item
  )
)&lt;/pre&gt;&lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84626?ContentTypeID=1</link><pubDate>Mon, 16 Aug 2021 16:51:14 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:f9a4ca6b-7a5a-437a-b516-0f5a076ba6d4</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;I digested this a little bit, and it appears that &amp;quot;expr&amp;quot; is either a mathematical expression or a reference to another expression based on the rowcode, so the rowcode should be replaced into that expression when it exists in the expr.&lt;/p&gt;
&lt;p&gt;Great case for a recursive function here.&amp;nbsp; This example assumes there are always 2 objects within the &amp;quot;expr&amp;quot; separated by +,-,* or /, and the rowcode references always contain the text &amp;quot;FRML&amp;quot;.&lt;/p&gt;
&lt;p&gt;Additionally, to assist I&amp;#39;ve created a new local!data2 variable which breaks out the expr into 2 separate parts and operator, easier to parse that way.&lt;/p&gt;
&lt;p&gt;Primary, rule!chris_21640():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;a!localVariables(
  local!data: {
    a!map(expr: &amp;quot;(123/12)&amp;quot;, rowcode: &amp;quot;FRML_88_1&amp;quot;),
    a!map(expr: &amp;quot;(1284*64)&amp;quot;, rowcode: &amp;quot;FRML_88_2&amp;quot;),
    a!map(expr: &amp;quot;(FRML_88_1+FRML_88_2)&amp;quot;, rowcode: &amp;quot;FRML_88_3&amp;quot;),
    a!map(expr: &amp;quot;(9090+122)&amp;quot;, rowcode: &amp;quot;FRML_88_4&amp;quot;),
    a!map(expr: &amp;quot;(FRML_88_3*FRML_88_4)&amp;quot;, rowcode: &amp;quot;FRML_88_5&amp;quot;)
  },
  local!operators: {&amp;quot;+&amp;quot;,&amp;quot;-&amp;quot;,&amp;quot;*&amp;quot;,&amp;quot;/&amp;quot;},
  local!data2: a!flatten(
    a!forEach(
      items: local!data,
      expression: {
        a!localVariables(
          local!row: fv!item.expr,
          local!rowcode: fv!item.rowcode,
          reject(
            fn!isnull,
            a!flatten(
              a!forEach(
                items: local!operators,
                expression: if(
                  search(fv!item,local!row)&amp;gt;0,
                  a!localVariables(
                    local!operator: fv!item,
                    local!items: split(stripwith(local!row,&amp;quot;()&amp;quot;),fv!item),
                    a!map(
                      expr1: local!items[1],
                      expr2: local!items[2],
                      operator: local!operator,
                      rowcode: local!rowcode
                    )
                  ),
                  null
                )
              )
            )
          )
        )
      }
    )
  ),
  
  a!forEach(
    items: local!data2,
    expression: rule!chris_21640_recur(
      data: local!data2,
      expr1: fv!item.expr1,
      expr2: fv!item.expr2,
      operator: fv!item.operator
    )
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And the helper recursive rule, rule!chris_21640_recur():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;concat(
  &amp;quot;(&amp;quot;,
  if(
    search(&amp;quot;FRML&amp;quot;,ri!expr1)&amp;gt;0,
    a!localVariables(
      local!index: wherecontains(ri!expr1,ri!data.rowcode),
      rule!chris_21640_recur(
        data: ri!data,
        expr1: index(ri!data.expr1,local!index,null),
        expr2: index(ri!data.expr2,local!index,null),
        operator: index(ri!data.operator,local!index,null),
      )
    ),
    ri!expr1,
  ),
  ri!operator,
  if(
    search(&amp;quot;FRML&amp;quot;,ri!expr2)&amp;gt;0,
    a!localVariables(
      local!index: wherecontains(ri!expr2,ri!data.rowcode),
      rule!chris_21640_recur(
        data: ri!data,
        expr1: index(ri!data.expr1,local!index,null),
        expr2: index(ri!data.expr2,local!index,null),
        operator: index(ri!data.operator,local!index,null),
      )
    ),
    ri!expr2,
  ),
  &amp;quot;)&amp;quot;
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This will return a list of mathematical expressions, replacing (recursively) rowcode references within the expressions.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Expression Rule</title><link>https://community.appian.com/thread/84620?ContentTypeID=1</link><pubDate>Mon, 16 Aug 2021 13:56:01 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:66f94bc7-986a-45ec-88e0-6e6680fbb091</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;Can you clarify what you&amp;#39;re trying to do here?&amp;nbsp; I&amp;#39;ve read this a few times now and I can&amp;#39;t quite figure out what ```&lt;span style="font-family:courier new, courier;"&gt;if &amp;quot;expr&amp;quot; of every Dictionary contains &amp;quot;rowcode&amp;quot;&lt;/span&gt;``` means.&amp;nbsp; From your screenshot it looks like every entry contains both &amp;quot;rowcode&amp;quot; &lt;em&gt;and&lt;/em&gt; &amp;quot;expr&amp;quot; properties.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>