Forum Discussion
Dicken
Post Prodigy
1 year agoInsert different table row or list range at each positon
Hi, Any suggestion about this i want to insert a tablle row or list range at varous positons, but a different value at each position ie 1st postion "A", 2nd "B" or whatever so , starting w...
- 1 year ago
List.Generate versions if interested.
This iterates the inserts and then grabs last one as output.
let alist = {"A".."Z"}, addPosition = {3,7,12}, // buffer if query output addVals = {1,2,3}, // buffer if query output genAdds = List.Generate( ()=>[ i = 0, l = alist ], each [i] <= List.Count(addPosition), each let curIndex = [i] in [ i = curIndex+1, l = List.InsertRange( [l], addPosition{curIndex}, {addVals{curIndex}} ) ], each [l] ), output = List.Last( genAdds ) in outputAnd here is a variation where inserts retain original positioning.
let alist = {"A".."Z"}, addPosition = {3,7,12}, // buffer if query output addVals = {1,2,3}, // buffer if query output addCount = List.Count(addPosition), genAdds = List.Generate( ()=>-1, each _ < addCount, each _ + 1, each [ isLast = _+1 = addCount, curPos=if _ = -1 then 0 else addPosition{_}, nextPos=if isLast then List.Count(alist) else addPosition{_+1}, count = nextPos-curPos, output = List.Range( alist, curPos, count ) & ( if isLast then {} else {addVals{_+1}} ) ][output] ), combineParts = List.Combine( genAdds ) in combineParts
Dicken
Post Prodigy
1 year agoThanks I'll have to work through your method but i do like generate; I did come up with tihis method,
let alist = {"A".."Z"} , pos = {7, 12, 16} , val ={"cat","dog","horse"} ,
index = {0..List.Count(pos)-1 }
in List.Accumulate( index, alist, (s,c)=> List.InsertRange( s, pos {c} , { val {c} } ) )Obviuosly you might want to subtract 1 from each postion, but it does work.