Skip to main content
April 15, 2025
Solved

Test

  • April 15, 2025
  • 5 replies
  • 0 views

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 [mention:b45a4f6a20b14a008e4e0ec732a8bf98:e9ed411860ed4f2ba0265705b8793d05] [mention:f5e81b8502fa4e77ad12697686a4fc56:e9ed411860ed4f2ba0265705b8793d05] [mention:2df2892a4df7486f9775b4d2f0e405fd:e9ed411860ed4f2ba0265705b8793d05] [mention:3f9551bc38d94a79ae62f9d520dbad6d:e9ed411860ed4f2ba0265705b8793d05] [mention:ba9672c2f5294cb98c0a2b8fb79cf967:e9ed411860ed4f2ba0265705b8793d05] 

Best answer by yashwantha0914

Hi [mention:8bcd3d18e9d94e72a9a13bf8a1e28c0e:e9ed411860ed4f2ba0265705b8793d05] 

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

5 replies

stefanhelzle0001
April 15, 2025

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/.../

yashwantha0914
April 15, 2025

Hi [mention:8bcd3d18e9d94e72a9a13bf8a1e28c0e:e9ed411860ed4f2ba0265705b8793d05] 

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

April 15, 2025

Great [mention:f0523105fd4d4e879f169a28f3dd0ec5:e9ed411860ed4f2ba0265705b8793d05] 
really appreciated 

stefanhelzle0001
April 15, 2025

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.

April 15, 2025

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"
    }
  )
}