How can I split up an array based on number sequences within it?

How can I split up an array based on number sequences within it?

Anonymous
Not applicable
1,254 Views
6 Replies
Message 1 of 7

How can I split up an array based on number sequences within it?

Anonymous
Not applicable

Hello,

I start with a large array of ascending number from 1 to 1000 sometimes more. Some of the numbers or groups of sequential numbers in that 1 to 1000 are missing creating gaps.
I need to collect the remaining numbers that are still in sequence to be saved out as their own new arrays.
Anyone have any suggestion on how to do that? I have no idea how you could identify when a gap in the sequence would appear.

thanks

 

0 Likes
Accepted solutions (1)
1,255 Views
6 Replies
Replies (6)
Message 2 of 7

blakestone
Collaborator
Collaborator

The below array splits the complete and incomplete sequence to their own arrays.

I am not sure if this is what you're after?
 

( 
    local numArray = #(
        "1,2,3,4,5",
        "1,4,5",
        "1,5,6,8,10"
    )
    
    local completeArray = #()
    local incompleteArray = #()
    
    for n in numArray do (
        local valid = true
        local f = filterString n ","
        
        for j=1 to f.count do (
            if f[j] as integer != j then valid = false
            if not valid then exit
        )
        
        if valid then append completeArray f else append incompleteArray f
    )
)
--------------------------------------------------------------------------------------
Technical 3D Graphic Artist
Autodesk 3dsMax 2015 - Service Pack 4
--------------------------------------------------------------------------------------
0 Likes
Message 3 of 7

Anonymous
Not applicable

I may not be explaining my question well.

In my array imagine a large ascending number sequence with numbers missing called myarray. There are numbers missing creating gaps which leave groups of ascending numbers that need to be put in their own seperate arrays. ?

example:

myarray = #(1,2,3,4,5,6,15,16,17,18,19,23,24,25,26,27,28)

In this example I need to generate 3 arrays that look like these

#(1,2,3,4,5,6)

#(15,16,17,18,19)

#(23,24,25,26,27,28)

The only things I will know before running the script is there will be a large ascending number sequence in the array of unknown size and that there will be missing numbers within it. The small remaining grouped sequences that remain need to be placed in new arrays.

 

 

 

0 Likes
Message 4 of 7

blakestone
Collaborator
Collaborator

Maybe this is what you're after?
You can access the multidimensional array like this: groupArray[1][1]

( 
	local numArray = #(1,2,3,4,5,6,15,16,17,18,19,23,24,25,26,27,28)
	local groupArray = #()
	local tempArray = #()
	
	for i=2 to numArray.count do (		
		if (i != numArray.count and numArray[i]-1 == numArray[i-1]) then (
			append tempArray numArray[i]
			continue
		)
		
		append groupArray tempArray
		tempArray = #()
	)

	print groupArray
)
--------------------------------------------------------------------------------------
Technical 3D Graphic Artist
Autodesk 3dsMax 2015 - Service Pack 4
--------------------------------------------------------------------------------------
0 Likes
Message 5 of 7

Anonymous
Not applicable

In the big array    #(1,2,3,4,5,6,15,16,17,18,19,23,24,25,26,27,28)

In your example code it creates 

#(2, 3, 4, 5, 6)
#(16, 17, 18, 19)
#(24, 25, 26, 27)

 

Looks like it skips the first number of each group.

1 is missing from the first array

15 missing from the second

23 missing from the third

0 Likes
Message 6 of 7

blakestone
Collaborator
Collaborator
Accepted solution

You're right, sorry about that!
The below example does what you're after.

 

( 
	local numArray = #(1,2,3,4,5,6,15,16,17,18,19,23,24,25,26,27,28)
	local groupArray = #()
	local tempArray = #(numArray[1])
	
	for i=2 to numArray.count do (
		if i == numArray.count then (
			append tempArray numArray[i]
		)
		else if numArray[i]-1 == numArray[i-1] then (
			append tempArray numArray[i]
			continue
		)
		
		append groupArray tempArray
		tempArray = #(numArray[i])
	)

	print groupArray
)
--------------------------------------------------------------------------------------
Technical 3D Graphic Artist
Autodesk 3dsMax 2015 - Service Pack 4
--------------------------------------------------------------------------------------
0 Likes
Message 7 of 7

Anonymous
Not applicable

awesome! I never would have got that! thank you!

0 Likes