Expression rule needs to keep running total among items, is foreach causing me issue?

Why would the following snippet in an expression rule not be able to correctly evaluate iterations past the first two?

with(
   local!datelisting: {"01/01/2020","01/02/2020","01/03/2020","01/04/2020"},
   local!BeginBalanceArray: {100.45,292,0,0},
   local!RunningTotal: 0,
   local!answerRecs: {
   a!forEach(items: local!datelisting,
             expression: with (
                               local!RunningTotal: local!RunningTotal + local!BeginBalanceArray[fv!index],
                               if ( fv!index > 1,
                                   (local!BeginBalanceArray[fv!index-1] + local!RunningTotal),
                                   local!BeginBalanceArray[fv!index] )
                         )
                    )
    },
    local!answerRecs
)

Testing the above gives a list in which the first two iterations seem to evaluate, but it 'forgets' running total after the second iteration:  

  • List of Number (Floating Point) - 4 items
      • 100.45(Number (Decimal))
        • 392.45(Number (Decimal))
          • 292(Number (Decimal))
            • 0(Number (Decimal))

          Also, instead of hardcoding for this simple example, I had tried the array functions updatearray() within the foreach and also append() with the 'BeginBalanceArray' up there initialized to only the first balance, but the array could not be modified in that case.  Is there a way to split this out among other expressions, etc?   Also tried different combinations of 'load' and 'localvariables', etc. but no dice.

            Discussion posts and replies are publicly visible

          Parents
          • I believe your code is working as expected. If your actual output should be 100.45, 392.45, 392.45, 392.45 (which I also assume should be) then please try the below code 

            a!localVariables(
              local!datelisting: {
                "01/01/2020",
                "01/02/2020",
                "01/03/2020",
                "01/04/2020"
              },
              local!BeginBalanceArray: {
                100.45,
                292,
                0,
                0
              },
              a!flatten(
                a!forEach(
                  items: local!datelisting,
                  expression: {
                    sum(
                      index(
                        local!BeginBalanceArray,
                        1 + enumerate(
                          fv!index
                        )
                      )
                    )
                  }
                )
              )
            )

          Reply
          • I believe your code is working as expected. If your actual output should be 100.45, 392.45, 392.45, 392.45 (which I also assume should be) then please try the below code 

            a!localVariables(
              local!datelisting: {
                "01/01/2020",
                "01/02/2020",
                "01/03/2020",
                "01/04/2020"
              },
              local!BeginBalanceArray: {
                100.45,
                292,
                0,
                0
              },
              a!flatten(
                a!forEach(
                  items: local!datelisting,
                  expression: {
                    sum(
                      index(
                        local!BeginBalanceArray,
                        1 + enumerate(
                          fv!index
                        )
                      )
                    )
                  }
                )
              )
            )

          Children