Get common values from more than two arrays

Hi ,

I have list of arrays coming from foreach, now i want unique elements for the list of arrays.

for example

localvariables(

local!arrays:foreach(

items:{5,6,7},

expresssion:enumerate(fv!item)+1

)

local!arrays

).

form this output,  i want unique elements from the list of arrays.

can anyone help me in this?

intersection is working only for separate arrays stored in each local variable but not for arrays executed in foreach.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Can you give us a more precise example of the overall set of input values you'd be dealing with, and the expected output?

    Also, please use a Code Box even when inserting code of just a few lines or so, for readability...

  • Hi Mike,

    Attaching the sampl code and output

    In the below screenshot you can see the local variable 'elements' having 3 items with list of integer arrays.

    Now I want unique elements from this list of arrays. Output should be 1,2,3,4,5 as these values are present in all the arrays.

  • 0
    Certified Lead Developer
    in reply to Druva

    You don't want "unique values" then, you want "values appearing in all".  "Unique Values" would be returned by a union() operation and in this case would return all values from 1 - 7.

    AFAIK intersection() accepts any number of arrays.

    Additionally it should be noted that when you create an Array of Arrays like the initial Elements list, Appian doesn't handle this very intuitively in some cases, often times acting more as if you're referring to a flattened list instead of calling individual elements.  For example if we try to union() just the 1st and 3rd arrays, we get something somewhat nonsensical like this:

    I notice that if you wrap each element in a!flatten(), it behaves better - maybe because it strips out the "list of any type" typecasting that the internal arrays tend to get (which tends to confuse things in cases like this) - receiving this output instead:

    Extending this to your need for the intersection() function, it seems to correct the behavior in a similar fashion:


    a!localVariables(
      local!elements: a!forEach(
        {6, 5, 7},
        enumerate(fv!item) + 1
      ),
      
      intersection(
        a!flatten(local!elements[1]),
        a!flatten(local!elements[2]),
        a!flatten(local!elements[3])
      )
    )

  • 0
    Certified Lead Developer
    in reply to Druva

    I am not sure what you actually want. Only values which are present in all the arrays? That would be intersection()

    Did you true the union() function?

    union(
      enumerate(5),
      5 + enumerate(5),
      10 + enumerate(5)
    )

  • Hi Mike,

    This works for me but the list of arrays are not fixed to keep all the arrays like flatten(local!elements[1]).

    We don't know how many list of integer arrays will come in foreach output.

    Can you help me in this

  • 0
    Certified Lead Developer
    in reply to Druva
    This works for me but the list of arrays are not fixed

    The issue here is, for intersection() and similar functions, the different arrays need to be passed in as separate parameters for the function to work (and not, like, a list of lists).  Thus I'm not sure how possible it is to call intersection on a list of lists of indefinite length, unless via some very advanced expression function trickery i'm not thinking of at the moment.

Reply Children
No Data