Notifications
Clear all

[Closed] array split every Nth

Here was a method used to split every other item in an array into two separate arrays.
Was curios to see if anyone had further improvements or better methods of doing this.


rays = #("object1","material1","object1","material2","object1","material3","object1","material4")

objs = #()
mats = #()

grp = 1
for i = rays.count to 1 by -1 do (
	if grp == 1 then
		append mats rays[i]
		else
		append objs rays[i]
	grp= if grp == 1 then 2 else 1
)	
clearlistener()

format "%" mats
format "%" objs

6 Replies

arrays = for k=1 to nums collect  #()
for k=0 to array.count-1 do append arrays[(mod k nums)+1] array[k+1]

Or in your case you can use something like this


 rays = #("object1","material1","object1","material2","object1","material3","object1","material4")
 dp = datapair #() #()
 for i in 1 to rays.count do (if mod i 2 != 0 then append dp.v1 rays[i] else append dp.v2 rays[i])
 format "objArr = %
mtlArr = %
" dp.v1 dp.v2
 

yet another option:


		local arr = for i = 1 to 1000 collect i as string
		local nums = 2		
		local arrays = for i = 1 to nums collect
		(
			for j = i to arr.count by nums collect arr[j]
		)

rays = #("object1","material1","object1","material2","object1","material3","object1","material4")
 
objs = #()
mats = #()
 
 
for i = 1 to  rays.count/2 do (
  mats[i] = rays[2*i]
 
 objs[i] = rays[2*i-1]
 
) 
clearlistener()
 
 

Nice ideas guys. I like ofer_z method quite a bit.
Great.

yes. i agree