Index Array with Nested arrays

Hello,

I'm having this problem:

I have to divide an array when 2 conditions are met (same name and country).

I've resolved this problem using "whereContains(firstCondition)" interescting it with "whereContains(Second Condition)"

Now i have this nested array:

Now i would like to index the Numbers Arrays into ri!array, but i can't find a way other then 2 nested a!forEach to fv!item every array.

How can i make the 2 nested a!forEach work?

Or do you have some other ideas?

Thanks

  Discussion posts and replies are publicly visible

Parents
  • Hi Gabriele,

    Can you explain your use case with example like 

    input : array1

    input : array2 

    output : ?

  • input: (ri!array)

    function for intersection:

    { load(
    local!whereName: a!forEach(
    items: union(ri!array.name,ri!array.name),
    expression: wherecontains(fv!item, ri!array.name),
    local!whereCountry: a!forEach(
    items: union(ri!array.countryId,ri!array.countryId),
    expression: wherecontains(fv!item,
    ri!array.countryId)),
    local!result: a!forEach(
    items: local!whereName,
    expression: rule!nestedIntersaction(numberArray: fv!item, arrayOfArrays: local!whereCountry) /*This rule makes the intersaction of the local variables*/
    ),
    local!result
    )}

    Where the result is:

     

    The result that i want now is:ri!array[SUB_ARRAY]

    For every Sub_Array of type "List of Number"

    The problem i find is in the fact that mine is a List of List of List of Number, with this it doesn't work a simple "a!forEach"

  • 0
    Certified Lead Developer
    in reply to Gabriele Camilli

    Some follow-up questions: what is the nature and contents of ri!array, and of ri!name?  Where and how is local!whereMa defined?  In plain terms, what are you trying to accomplish with this code?  I think it might be more helpful if we can step back and see if this can be simplified somewhat, which I suspect is true but it's hard to know without more details.

  • ri!array is an array of cdt.

    The aim would be to subdived this array into sub arrays that have the same name and country.

    What i thought is to get the interesaction of where the values of country is the same with where the name are the same.

    The data is retrivable also by a query rule, but i would have the same problem of cycling for all the countries and names in the cdt Array (ri!array)

    The end goal would be to have a List of List of the cdt, a list of (ri!array) divided simultaneously by name and country

  • 0
    Certified Lead Developer
    in reply to Gabriele Camilli

    Thanks for clarifying.  Should I assume that the one instance of ri!name in your example code should actually read as ri!array also?  I see that local!whereMa was intended to be local!whereName, thanks for fixing that.

    Can you perhaps provide a small example of an input value for ri!array, and what your expected end result would be given that input?

  • ri!array (i discarded some field to make it readable):

    List of Dictionary: 12 items

    Dictionary

    name: "a" countryId: "201" productName: "ex1" packSize: "20 tablets"

    Dictionary

    name: "a" countryId: "201" productName: "ex2" packSize: "12 tablets"

    Dictionary

    name: "b" countryId: "121" productName: "bel21" packSize: "21 tablets"

    Dictionary

    name: "c" countryId: "121" productName: "p18A" packSize: "18 tablets"

    Dictionary

    name: "c" countryId: "121" productName: "p24A" packSize: "24 tablets"

    Dictionary

    name: "c" countryId: "52" productName: "p27A" packSize: "27 tablets"

    Dictionary

    name: "c" countryId: "121" productName: "p27B" packSize: "27 tablets"

    Dictionary

    name: "c" countryId: "52" productName: "p18B" packSize: "18 tablets"

    Dictionary

    name: "c" countryId: "52" productName: "p24B" packSize: "24 tablets"

    Dictionary

    name: "b" countryId: "12" productName: "bel42" packSize: "42 tablets"

    Dictionary

    name: "a" countryId: "121" productName: "ex4" packSize: "10 tablets"

    Dictionary

    name: "a" countryId: "121" productName: "ex5" packSize: "6 tablets"

    Raw{{name: "a", countryId: "201", productName: "ex1", packSize: "20 tablets"}, {name: "a", countryId: "201", productName: "ex2", packSize: "12 tablets"}, {name: "b", countryId: "121", productName: "bel21", packSize: "21 tablets"}, {name: "c", countryId: "121", productName: "p18A", packSize: "18 tablets"}, {name: "c", countryId: "121", productName: "p24A", packSize: "24 tablets"}, {name: "c", countryId: "52", productName: "p27A", packSize: "27 tablets"}, {name: "c", countryId: "121", productName: "p27B", packSize: "27 tablets"}, {name: "c", countryId: "52", productName: "p18B", packSize: "18 tablets"}, {name: "c", countryId: "52", productName: "p24B", packSize: "24 tablets"}, {name: "b", countryId: "12", productName: "bel42", packSize: "42 tablets"}, {name: "a", countryId: "121", productName: "ex4", packSize: "10 tablets"}, {name: "a", countryId: "121", productName: "ex5", packSize: "6 tablets"}}

    The result should be:
    {
    {name: "a", countryId: "201",productName: "ex1", packSize: "20 tablets"},
    {name: "a", countryId: "201",productName: "ex2", packSize: "12 tablets"}
    },
    {
    {name: "a", countryId: "121",productName: "ex4", packSize: "10 tablets"},
    {name: "a", countryId: "121",productName: "ex5", packSize: "6 tablets"}
    },
    {
    {name: "b", countryId: "121",productName: "bel21", packSize: "21 tablets"}
    },
    {
    {name: "b", countryId: "15",productName: "bel42", packSize: "42 tablets"}
    },
    { {name: "c", countryId: "121",productName: "p18A", packSize: "18 tablets"},
    {name: "c", countryId: "121",productName: "p24A", packSize: "24 tablets"},
    {name: "c", countryId: "121",productName: "p27B", packSize: "27 tablets"}

    },
    {
    {name: "c", countryId: "52",productName: "p27A", packSize: "27 tablets"},
    {name: "c", countryId: "52",productName: "p18B", packSize: "18 tablets"},
    {name: "c", countryId: "52",productName: "p24B", packSize: "24 tablets"}
    }
    Thanks
  • +1
    Certified Lead Developer
    in reply to Gabriele Camilli

    Consider this alternate implementation (i made up my own structure for ri!array as you hadn't replied yet, but I think it's similar enough to fit into your data model)

    a!localVariables(
      local!array: {
        {
          item: "stapler",
          name: "Mike",
          countryId: 1
        },
        {
          item: "printer",
          name: "Larry",
          countryId: 2
        },
        {
          item: "laptop",
          name: "Mike",
          countryId: 1
        },
        {
          item: "glass",
          name: "Mike",
          countryId: 3
        }
      },
      
      local!allNameCountryPairs: a!forEach(
        local!array,
        {
          name: fv!item.name,
          countryId: fv!item.countryId
        }
      ),
      local!uniqueNameCountryPairs: union(local!allNameCountryPairs, local!allNameCountryPairs),
    
      a!forEach(
        local!uniqueNameCountryPairs,
        a!localVariables(
          local!currentEntry: fv!item,
          a!forEach(
            local!array,
            if(
              and(
                local!currentEntry.name = fv!item.name,
                local!currentEntry.countryId = fv!item.countryId
              ),
              fv!item,
              {}
            )
          )
        )
      )
    )

    Here, instead of worrying about grabbing indices for the unique names / country IDs and trying to do a complicated extraction of the overlaps, we instead simply get a list of all unique name/countryId pairs first, then iterate over that list and (inside) iterate over the original list and return any entries where the values match.   Output below:

  • ri!array is in this form (i removed some fields to make it readable, there would be many more):

    {

    {name: "a", countryId: "201", productName: "ex1", packSize: "20 tablets"},

    {name: "a", countryId: "201", productName: "ex2", packSize: "12 tablets"},

    {name: "a", countryId: "121", productName: "ex4", packSize: "10 tablets"},

    {name: "a", countryId: "121", productName: "ex5", packSize: "6 tablets"},

    {name: "b", countryId: "121", productName: "bel21", packSize: "21 tablets"},

    {name: "b", countryId: "15", productName: "bel42", packSize: "42 tablets"},

    {name: "c", countryId: "121", productName: "p18A", packSize: "18 tablets"},

    {name: "c", countryId: "121", productName: "p24A", packSize: "24 tablets"},

    {name: "c", countryId: "121", productName: "p27B", packSize: "27 tablets"},

    {name: "c", countryId: "52", productName: "p27A", packSize: "27 tablets"},

    {name: "c", countryId: "52", productName: "p18B", packSize: "18 tablets"},

    {name: "c", countryId: "52", productName: "p24B", packSize: "24 tablets"}

    }

    The division should be:

    {
    {
    {name: "a", countryId: "201",productName: "ex1", packSize: "20 tablets"},
    {name: "a", countryId: "201",productName: "ex2", packSize: "12 tablets"}
    },
    {
    {name: "a", countryId: "121",productName: "ex4", packSize: "10 tablets"},
    {name: "a", countryId: "121",productName: "ex5", packSize: "6 tablets"}
    },
    {
    {name: "b", countryId: "121",productName: "bel21", packSize: "21 tablets"}
    },
    {
    {name: "b", countryId: "15",productName: "bel42", packSize: "42 tablets"}
    },
    { {name: "c", countryId: "121",productName: "p18A", packSize: "18 tablets"},
    {name: "c", countryId: "121",productName: "p24A", packSize: "24 tablets"},
    {name: "c", countryId: "121",productName: "p27B", packSize: "27 tablets"}

    },
    {
    {name: "c", countryId: "52",productName: "p27A", packSize: "27 tablets"},
    {name: "c", countryId: "52",productName: "p18B", packSize: "18 tablets"},
    {name: "c", countryId: "52",productName: "p24B", packSize: "24 tablets"}
    }
    }

  • 0
    Certified Lead Developer
    in reply to Gabriele Camilli

    My last reply (as well as yours) have apparently disappeared - did you have a chance to see it?

    edit: restored, thanks!

  • No, sorry, what about you?

    It was flagged as "Spam", don't know why

Reply Children
No Data