Test

Here is the use case  Name1 is the section names value1 is the text value,when next section name starts with item2 before empty section names consider as section names with the text followed by as mentioned  in the output 

a!localVariables(
  local!testdata: {
    { "Name1": "Item1", "Value1": "Data for Item 1" },
    { "Name1": null, "Value1": "Different data for Item 2" },
    { "Name1": null, "Value1": "Some other kind of data" },
    { "Name1": "Item2", "Value1": "Another example of data" },
    { "Name1": null, "Value1": "Random data like GBP 50,000" },
    { "Name1": "Item3", "Value1": "Data related to an event or value" },
    { "Name1": null, "Value1": "Adjustable by percentage of revenue" },
    { "Name1": null, "Value1": "Premium payable quarterly" }
  },
  
  local!testdata


expected output is 

[
{ "section": "Item1", "fullText": "Data for Item 1 Different data for Item 2 Some other kind of data" },
{ "section": "Item2", "fullText": "Another example of data Random data like GBP 50,000" },
{ "section": "Item3", "fullText": "Data related to an event or value Adjustable by percentage of revenue Premium payable quarterly" }
]

@Stefan Helzle      

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    I think this can be solved using the reduce function. In a blog post, I describe how to implement such algorithms. Specifically how the create stable shared values across iterations.

    appian.rocks/.../

  • +1
    Certified Senior Developer

    Hi  

    a!localVariables(
      local!testdata: {
        {
          "Name1": "Item1",
          "Value1": "Data for Item 1"
        },
        {
          "Name1": null,
          "Value1": "Different data for Item 2"
        },
        {
          "Name1": null,
          "Value1": "Some other kind of data"
        },
        {
          "Name1": "Item2",
          "Value1": "Another example of data"
        },
        {
          "Name1": null,
          "Value1": "Random data like GBP 50,000"
        },
        {
          "Name1": null,
          "Value1": "Hii data like GBP 50,000"
        },
        {
          "Name1": "Item3",
          "Value1": "Data related to an event or value"
        },
        {
          "Name1": null,
          "Value1": "Adjustable by percentage of revenue"
        },
        {
          "Name1": null,
          "Value1": "Premium payable quarterly"
        },
        {
          "Name1": null,
          "Value1": "party payable quarterly"
        },
        {
          "Name1": "Item4",
          "Value1": "Item four data"
        }
      },
      local!uniqueName: reject(
        fn!isnull,
        union(
          local!testdata.Name1,
          local!testdata.Name1
        )
      ),
      local!indexes: a!forEach(
        items: enumerate(length(local!uniqueName)) + 1,
        expression: a!localVariables(
          local!first: index(local!uniqueName, fv!item, {}),
          local!second: index(local!uniqueName, fv!item + 1, null),
          if(
            a!isNullOrEmpty(local!second),
            {},
            a!map(
              startIndex: index(
                wherecontains(
                  tostring(local!first),
                  touniformstring(local!testdata.Name1)
                ),
                1,
                ""
              ),
              endIndexPlusOne: index(
                wherecontains(
                  tostring(local!second),
                  touniformstring(local!testdata.Name1)
                ),
                1,
                ""
              )
            )
          )
        )
      ),
      local!finalNameStartIndex: local!indexes[length(local!indexes)].endIndexPlusOne,
      a!flatten(
        {
          a!forEach(
            items: local!indexes,
            expression: a!map(
              section: index(
                local!testdata.Name1,
                fv!item.startIndex,
                ""
              ),
              fullText: joinarray(
                index(
                  local!testdata.Value1,
                  enumerate(
                    tointeger(fv!item.endIndexPlusOne) - fv!item.startIndex
                  ) + fv!item.startIndex,
                  ""
                ),
                " "
              )
            )
          ),
          a!map(
            section: index(
              local!testdata.Name1,
              local!finalNameStartIndex,
              ""
            ),
            fullText: joinarray(
              index(
                local!testdata.Value1,
                {
                  enumerate(
                    length(local!testdata) - local!finalNameStartIndex + 1
                  ) + local!finalNameStartIndex
                },
                ""
              ),
              " "
            )
          )
        }
      )
    )

    Hope it helps. please test it with some scenarios; let me know is it working fine

  • 0
    Certified Lead Developer

    OK, then here is my solution. There are very simple alternatives to foreach(),

    This helper expression either updates the last item in the list and concatenates the text, or adds a new section item. The created value is then passed to the next iteration by the reduce() function.

  • 0
    Certified Senior Developer

    If the other answers don't get you what you're looking for you could always change the way you define local!testData to be a list of map like below.

    local!testData: {
      a!map(
        section: "Item 1", 
        fullText: {
          "Data for Item 1",
          "Different data for Item 2",
          "Some other kind of data"
        }
      ),
      a!map(
        section: "Item 2", 
        fullText: {
          "Another example of data",
          "Random data like GBP 50,000"
        }
      ),
      a!map(
        section: "Item 3", 
        fullText: {
          "Data related to an event or value",
          "Adjustable by percentage of revenue",
          "Premium payable quarterly"
        }
      )
    }