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
Discussion posts and replies are publicly visible
Like a lot of problems, you can break this down into smaller problems to solve.
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.
My apologies, actually its not just array of integers but array of CDTs. I have updated the question above.
Please help me with it.
Why not just add the names and then renumber the whole list? That would be easy to do with a foreach and a!update.
Can you help me understand with an example, please ?
Hm ... this really depends on your actual use case.
Where is that data coming from? Is this the whole list or just a part of it? What is the meaning of that "rollNumber"? Is it used in other table as a foreign key or something? Sorting?
Then, how is that data added? By a user in an interface?
To answer your questions, please have a look on the inline comments :
Where is that data coming from? [ Data is coming form a table in database ]
Is this the whole list or just a part of it? [ it is the whole list ]
What is the meaning of that "rollNumber"? [ '"rollNumber" is an integer value field]
Is it used in other table as a foreign key or something? Sorting? [ It is not used in other table as foreign key, but used to sort the whole table data]
Then, how is that data added? By a user in an interface? [ data is added from a dropdown in an interface]
Hope this answers your question.
OK. So to keep the data in a specific order, the actual values do not matter as long as they are ascending and unique.
A code snippet similar to the below, should update the rollNumbers. There is no complex logic necessary.
a!forEach( items: local!list, expression: a!update( fv!item, "rollNumber", fv!index ) )