Hi All,
Need some help with writing a logic.
I have an integer array {1,2,3,4,5,6,7,8,9,10,11,12}.
I want an output as three arrays {1,2,3,4,5}, {6,7,8,9,10}, {11,12}
Any function or quick work around to split an array by grouping first five elements in an array, next five elements in array and so on
TIA
Arun
Discussion posts and replies are publicly visible
This should do
Just change the local!inputArray and local!maximumPerArray for other results.
a!localVariables( local!inputArray: {1,2,3,4,5,6,7,8,9,10,11,12}, local!totalArray: length(local!inputArray), local!maximumPerArray: 5, local!amountArrays: ceiling(local!totalArray / local!maximumPerArray), local!iterationArray: enumerate(local!amountArrays), a!forEach( items: local!iterationArray, expression: with( local!leftAmountToCut: fv!item * local!maximumPerArray, local!rightAmountToCut: local!totalArray - ((fv!item + 1) * local!maximumPerArray), rdrop( ldrop( local!inputArray, local!leftAmountToCut ), if(local!rightAmountToCut < 0, 0, local!rightAmountToCut) ) ) ) )
May I ask why you need to do that?
Tried to reduce the lines of code a bit ...
a!localVariables( local!list: floor(rand(23)*100), local!max: 5, a!forEach( items: enumerate(ceiling(length(local!list) / local!max)), expression: a!forEach( items: fv!item * local!max + 1 + enumerate(local!max), expression: index(local!list, fv!item, {}) ) ) )
Nice reduction. And the cost still the same O(n) if not better (depending on the rdrop and ldrop implementations).Awesome.But, why the rand(23)?
Thanks. This is just my self containing example. I think you would turn both locals into rule inputs.
Yeap, put on the code just to everything be visible on the example.
that helps ! Thanks
hi just to extend on this question which I need to clarify:for this sample array: local!list: { { id: 1, text: "first text", name: "Ada" }, { id: 2, text: "2nd text", name: "ben" }, { id: 3, text: "3rd text", name: "sam" } },what is the best way to separate the array into sub arrays, using a specific index eg. 2 then I should only get local!newList: {{id: 1,text: "first text",name: "Ada"},{id: 2,text: "2nd text",name: "ben"}}
Does this help ?
a!localVariables( local!list: { { id: 1, text: "first text", name: "Ada" }, { id: 2, text: "2nd text", name: "ben" }, { id: 3, text: "3rd text", name: "sam" } }, index( local!list, 1 + enumerate( 2/* the specific index value*/ ) ) )
yes thanks @siddharth_m
Hi Stefan,
I have 2 Questions.
1. There are 2 numbers. one start number and one end number. in between those numbers, we have to find the one number?
example: my start number is 100, end number is 200
find number : 5
output: 105, 115, 125, 135, 145, 150,151,152,153,154,155,156,157,158,159,165,175,185,195
2. my input number is 5
output : 1 22 333 4444
can you pls give the code for these 2 questions?