<?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>Reusing Local Variable</title><link>https://community.appian.com/discussions/f/general/27143/reusing-local-variable</link><description>Hi Everyone, 
 
 I have a requirement where i can reuse local variable array. For Example 
 
 a!localvariable( 
 local!iterations:{1,2,3}, 
 local!value, 
 a!foreach( 
 items:local!iterations, 
 expression{ 
 append( 
 local!value, 
 fv!index 
 )})) </description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106715?ContentTypeID=1</link><pubDate>Fri, 13 Jan 2023 07:39:10 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:b3bb1777-b4e1-4fb2-ba87-5f381f8ddfba</guid><dc:creator>Tarun</dc:creator><description>&lt;p&gt;Yes local!value is being reused in your example. And hence you got a list of 3 items towards the end of it. If it were not reused, you would&amp;#39;ve got just 1 item in result --&amp;gt; 3.&lt;/p&gt;
&lt;p&gt;And in your output you mentioned --&amp;gt;&amp;nbsp;&lt;/p&gt;
[quote userid="57059" url="~/discussions/f/general/27143/reusing-local-variable"]&lt;p&gt;&lt;span&gt;1st Iteration&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2nd Iteration&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;3rd Iteration&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;3.&lt;/span&gt;&lt;/p&gt;[/quote]
&lt;p&gt;Either you have misinterpreted the output or I understood your output in a different context. Its not iteration. Its just an item that&amp;#39;s appended to the list. So in memory --&amp;gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;After 1st Iteration --&amp;gt; Local!value = {1}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;After 2nd&amp;nbsp;Iteration --&amp;gt; Local!value = {1,2}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;After 3rd&amp;nbsp;Iteration --&amp;gt; Local!value = {1,2,3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Once the loop is finished, {1,2,3} is returned. You can see now that local!value is reused.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106710?ContentTypeID=1</link><pubDate>Fri, 13 Jan 2023 06:24:30 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:e83de830-d4b5-4a4f-bdc2-d226b27b185b</guid><dc:creator>aryan</dc:creator><description>&lt;p&gt;Can you please specify the use case for doing so&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106575?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 15:28:50 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:25edca7e-8068-4cfe-ab23-690daf100bae</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;I think you have a fundamental misunderstanding of what local variables are and what they do (and what they don&amp;#39;t do).&amp;nbsp; And what happens when functions act upon them.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A slightly simpler example:&lt;/em&gt;&amp;nbsp; If you have a local variable of &amp;quot;local!asdf: {1, 2, 3}&amp;quot; for instance - and you call &lt;em&gt;append()&lt;/em&gt; on it:&amp;nbsp; the result of &lt;em&gt;append()&lt;/em&gt; is simply an output of the resulting value, it does not (nor would it ever) modify the original local variable it was acting upon.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106543?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 11:02:27 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0b1cd159-db36-4dca-909b-580eabe4c5bf</guid><dc:creator>Gopu Hema</dc:creator><description>&lt;p&gt;&lt;span&gt;Thanks!! But its not the exact code or the way that i need, I just want to reuse the variable in next iteration which is updated. But Thanks for the Info&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106542?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 11:02:02 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:fb53f21b-1e50-46e0-8871-dde1b5074be2</guid><dc:creator>Gopu Hema</dc:creator><description>&lt;p&gt;Thanks!! But its not the exact code or the way that i need, I just want to reuse the variable in next iteration which is updated. But Thanks for the Info&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106524?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 09:11:06 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3fcab0c2-38ef-4505-b8bd-b653de43cccc</guid><dc:creator>Harshitha Mangamuri</dc:creator><description>&lt;p&gt;Hi Hema, I hope this will help you in solving your issue.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!iterations: {1,2,3},
  local!res: a!forEach(
    items: local!iterations,
    expression: {
      a!forEach(
        items: enumerate(fv!index)+1,
        expression: local!iterations[fv!item]
      )
    }
  ),
  local!res
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106523?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 08:57:42 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:27f285a9-9a49-45a3-8811-b3dc74c86bb8</guid><dc:creator>Alex Boerescu</dc:creator><description>&lt;p&gt;That is the correct behaviour - you cannot modify the value of variables outside of a foreach. You could nest two foreach to achieve this - see the code below.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localvariables(
  local!iterations: { 1, 2, 3 },
  a!forEach(
    items: local!iterations,
    expression: a!localVariables(
      local!index: fv!index,
      a!forEach(
        items: enumerate(local!index),
        expression: local!iterations[fv!index]
      )
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reusing Local Variable</title><link>https://community.appian.com/thread/106522?ContentTypeID=1</link><pubDate>Wed, 11 Jan 2023 08:44:02 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9c59e053-f84e-44ea-853a-563641cfbf56</guid><dc:creator>Harshit Bumb (Appyzie)</dc:creator><description>&lt;p&gt;You CANNOT update the value of a variable in an expression rule. To update a variable value, you need a user interaction to happen and that is why it is only possible in an interface or a PM.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;To achieve your use case, you can make this small tweak in your code and it should work.&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!iterations: { 1, 2, 3 },
  local!value,
  a!forEach(
    items: local!iterations,
    expression: enumerate(fv!index)+1
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>