Skip to main content
November 5, 2021
Question

Update Array Recursively

  • November 5, 2021
  • 7 replies
  • 0 views

Hi,

I am trying to update an array of CDTs recursively.

Base Array : 

{
{name:"Alex",rollNumber:null},
{name:"Stephen",rollNumber:3},
{name:"Dave",rollNumber:4},
{name:"Steave",rollNumber:5},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
}

Now, If I insert number 4  where rollNumber is null , if that number exists in CDT array then all numbers greater than 4 should increase by1 until all duplicates are change to unique number, but all numbers less than 4 should remain unchanged.

This is how numbers should change :

4->5,

5->6

Since there are no duplicates after 6 then increment should stop here,

Updated Array: {
{name:"Stephen",rollNumber:3},
{name:"Alex",rollNumber:4},
{name:"Dave",rollNumber:5},
{name:"Steave",rollNumber:6},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
},

Please help me with it

    7 replies

    stewart.burchell
    November 5, 2021

    Like a lot of problems, you can break this down into smaller problems to solve.

    1. solve how to take you array and insert ONE new value into it
      1. solve inserting a value that doesn't yet exist in the array
      2. solve inserting a value that does exist in the array
    2. solve how to invoke the above for a list of values

    For 1 you can write an expression rule that takes as its input your array of integers that you want to modify and a single integer that you want to insert into the array. 

    For 2 there is the reduce() function which you can use to recursively call your new expression.

    November 6, 2021

    My apologies, actually its not just array of integers but array of CDTs. I have updated the question above.

    Please help me with it.

    stefanhelzle0001
    Brainy
    November 6, 2021

    Why not just add the names and then renumber the whole list? That would be easy to do with a foreach and a!update.