Reusing Local Variable

Certified Senior Developer

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!!!

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Associate Developer
    in reply to Gopu Hema

    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.

Children
No Data