Skip to main content
January 11, 2023
Question

Reusing Local Variable

  • January 11, 2023
  • 8 replies
  • 0 views

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

)}))

The output i need is

1st Iteration

1

2nd Iteration

1

2

3rd Iteration

1

2

3

But the output the code is generating is

1st Iteration

1

2nd Iteration

2

3rd Iteration

3.

I thought that local variable should be append with the value and  can use for next iteration but it is initializing every time with null value rather than appendvalue. Could you please help me to get the output i need

Thanks In Advance!!!

    8 replies

    harshitb6843
    January 11, 2023

    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. 

    To achieve your use case, you can make this small tweak in your code and it should work.

    a!localVariables(
      local!iterations: { 1, 2, 3 },
      local!value,
      a!forEach(
        items: local!iterations,
        expression: enumerate(fv!index)+1
      )
    )

    gopuhAuthor
    January 11, 2023

    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

    mikes0011
    Brainy
    January 11, 2023

    I think you have a fundamental misunderstanding of what local variables are and what they do (and what they don't do).  And what happens when functions act upon them.

    A slightly simpler example:  If you have a local variable of "local!asdf: {1, 2, 3}" for instance - and you call append() on it:  the result of append() is simply an output of the resulting value, it does not (nor would it ever) modify the original local variable it was acting upon.

    The Expression Guru
    alexandru.boerescu
    January 11, 2023

    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.

    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]
          )
        )
      )
    )

    January 11, 2023

    Hi Hema, I hope this will help you in solving your issue.

    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
    )

    gopuhAuthor
    January 11, 2023

    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

    January 13, 2023

    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've got just 1 item in result --> 3.

    And in your output you mentioned --> 

    1st Iteration

    1

    2nd Iteration

    2

    3rd Iteration

    3.

    Either you have misinterpreted the output or I understood your output in a different context. Its not iteration. Its just an item that's appended to the list. So in memory --> 

    After 1st Iteration --> Local!value = {1}

    After 2nd Iteration --> Local!value = {1,2}

    After 3rd Iteration --> Local!value = {1,2,3}

    Once the loop is finished, {1,2,3} is returned. You can see now that local!value is reused.

    Inspiring
    January 13, 2023

    Can you please specify the use case for doing so