Notifications
Clear all

[Closed] Manipulatign an array – nested arrays by items difference

The title is not very accurate, but I don’t know how to describe what I want in one short sentence. So,

How from this:


#(0, 10, 11, 12, 13, 14, 20, 30, 31, 40, 50, 60, 61, 62, 70, 71, 80, 81, 82, 83, 84, 85, 86, 87, 90, 91)

to get this:


#(1, #(2, 3, 4, 5, 6), 7, #(8, 9), 10, 11, #(12, 13, 14), #(15, 16), #(17, 18, 19, 20, 21, 22, 23, 24), #(25, 26)) 

The idx of the neighbor items are in sub-arrays.

I use this, but maybe there is a clever and shorter way to receive the same result.


(
	arr = #(0, 10, 11, 12, 13, 14, 20, 30, 31, 40, 50, 60, 61, 62, 70, 71, 80, 81, 82, 83, 84, 85, 86, 87, 90, 91) 
	--	final result have to be:
	--	#(1, #(2, 3, 4, 5, 6), 7, #(8, 9), 10, 11, #(12, 13, 14), #(15, 16), #(17, 18, 19, 20, 21, 22, 23, 24), #(25, 26)) 
	buddyArr = #()
	for i = 1 to arr.count - 1 do
	(
		k = 0
		tmpArr = #()
		for j = i + 1 to arr.count do
		(
			if arr[j] == arr[i] + 1 + k do 
			(
				appendifUnique tmpArr i
				appendifUnique tmpArr j
				k += 1				
			)
		)
		if tmpArr.count != 0 do append buddyArr tmpArr
	)
	format "1buddyArr: % 
" buddyArr
	--	"2,3,4,5,6," "3,4,5,6" "4,5,6" "5,6" 8,9" "12,13,14" "13,14"...
	uniqueArr = #()
	for i =  1 to buddyArr.count - 1 do
	(
		for j = i + 1 to buddyArr.count do
		(
			if (findItem buddyArr[j] buddyArr[i][(buddyArr[i].count)]) != 0 do
				appendIfUnique uniqueArr j
		)
	)
	-- format "uniqueArr: % 
" uniqueArr
	for i = uniqueArr.count to 1 by -1 do deleteItem buddyArr uniqueArr[i]
	format "2buddyArr: % 
" buddyArr
	newArr = #()
	for i = 1 to arr.count do
	(
		appendSingle = true
		stopLoop = false
		for j = 1 to buddyArr.count while stopLoop == false do
		(
			if (findItem buddyArr[j] i) == 0 then
			(
				--	"do nothing"
			)
			else
			(
				stopLoop = true
				appendSingle = buddyArr[j]
			)
		)
		if appendSingle == true then
			append newArr i
		else
			appendIfUnique newArr appendSingle
	)
	format "newArr: % 
"  newArr
)

8 Replies

In these cases, the use of while is better than for-loop.


arr = #(0, 10, 11, 12, 13, 14, 20, 30, 31, 40, 50, 60, 61, 62, 70, 71, 80, 81, 82, 83, 84, 85, 86, 87, 90, 91) 

arrNew = #()
index = 0

while index < arr.count do
(
	index +=1
	arrTemp = #(index)
	consecutive = true
	
	while consecutive do
	(
		index += 1
		if arr[index] == arr[index-1] +1 then append arrTemp index else consecutive = false
	)
	
	if arrTemp.count == 1 then append arrNew arrTemp[1] else append arrNew arrTemp
	index -= 1
)

format "arrNew= %
" arrNew

2 Replies
(@miauu)
Joined: 11 months ago

Posts: 0

Thank you.
I’ve tried several times to strat with while loop, but with no success.

(@denist)
Joined: 11 months ago

Posts: 0

???

arr = #(0, 10, 11, 12, 13, 14, 20, 30, 31, 40, 50, 60, 61, 62, 70, 71, 80, 81, 82, 83, 84, 85, 86, 87, 90, 91) 
(
	new = #(#(1))
	for k=2 to arr.count do
	(
		if arr[k-1] != arr[k]-1 then append new #(k) else append new[new.count] k
	)
	for k=1 to new.count where new[k].count == 1 do new[k] = new[k][1] 
	new
)




I have edit the code to shorten it.

Thank you.

I was sure that it can be done with less lines than my attempt. Since I’ve became a father 12 days ago my brain is not so sharp and some trivial tasks takes me more time than I want to spend.

this fact and the task with arrays are related anyhow, aren’t they?

anyway … my congratulations!

Congrats Miauu! Your child is for sure your best script! :applause:

By the way, I was sure that DenisT will do it with for-loop… in just one line. He has used two. :bowdown:

Thank you. Yes, it is related anyhow.

Thank you.
He is also my second best render(my second son).