array help for a disarrayed mind

I'm having trouble with this problem

I have an array like so

#(20, 21, 22, 23, 27, 28, 29, 30)

I need to find min and max values of consecutive elements

so my function would give me (20,23) and (27,30)

Here's what I have so far but i'm not doing it right.

for i = 1 to coneLocTimes.count do (
--coneLocTimes #(20, 21, 22, 23, 27, 28, 29, 30) --example array no need to sort as its already sorted
if conelocTimes[i] == (conelocTimes[i+1] - 1) then --test the first element in the array and see if the next element is consecutive
(
valpt1 = conelocTimes[i]
print i
-- keep looping throught the values until we find one that is none consecutive
for i = 1 to coneLocTimes.count do ( -- start of second loop

if conelocTimes[i] != conelocTimes[i+1] then
(
valpt2 = conelocTimes[i]
)

) -- end of second loop

)
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
lucky_lowell's picture

thanks Anubis

The FilterString function is perplexing to me...
I looked it up and I just don't see how it evaluates to
#("20", "23", "27", "30")

intArr = #(20, 21, 22, 23, 27, 28, 29, 30)
#(20, 21, 22, 23, 27, 28, 29, 30)

splitArr = FilterString ((intArr as BitArray) as String) "#{, .}"
#("20", "23", "27", "30")

Is there somewhere else in the maxscript reference I should be looking?

array of strings filterString string token_string [splitEmptyTokens:]

Anubis's picture

nested action

Yeah, that line:
splitArr = FilterString ((intArr as BitArray) as String) "#{, .}"
actually do a few thinks at once, and they're nested in one line with brackets. If I unfold it, i.e. write the same on steps, it may like --

bArr = intArr as BitArray
sArr = bArr as String
splitArr = FilterString sArr "#{, .}"

If you execute this line by line you'll see the 'magic' ;)

my recent MAXScripts RSS (archive here)

lucky_lowell's picture

Anubis thanks. BitArray

Anubis thanks.
BitArray function.... Wow

Anubis's picture

I like your description

I like your description ("disarrayed mind") ;)
Here is something working:

intArr = #(20, 21, 22, 23, 27, 28, 29, 30)
 
splitArr = FilterString ((intArr as BitArray) as String) "#{, .}"
splitArr = for i in splitArr collect i as Integer
 
subArrs = #()
for i = 1 to splitArr.count by 2 do (
	append subArrs (for j = splitArr[i] to splitArr[i+1] collect j)
)
 
subArrs -->> #(#(20, 21, 22, 23), #(27, 28, 29, 30))
 
-- EDIT: Ah, you ask just for min/max, so here is:
--------------------------------------------------
intArr = #(20, 21, 22, 23, 27, 28, 29, 30)
 
splitArr = FilterString ((intArr as BitArray) as String) "#{, .}"
splitArr = for i in splitArr collect i as Integer
 
subArrs = #()
for i = 1 to splitArr.count by 2 do (
	append subArrs #(splitArr[i], splitArr[i+1])
)
subArrs -->> #(#(20, 23), #(27, 30))

my recent MAXScripts RSS (archive here)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.