basic array question

hi.. maybe someone can help me with this one..

i am not figuring out how to use the find item on an array of coordinates..
what i want to do is find the item that have the especific z coordinate, doesnt matter whats the x or y
so i am trying something like this:

_array = #([1,2,3],[4,5,6],[7,8,9])

now i want to find the item with z == 6

i´ve tried
findItem _array 6 -- returns 0
findItem _array [*,*,6] -- Syntax error

thanks

Comments

Comment viewing options

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

hi

using Collect() is good enough, especially if you like to get all matched indexes. Max's FindItem() resurns only the index of the first occurrence of the given value. So, if you remove last line of the next function you'll modify it to return an array of all matched indexes --

-- example array:
_array = #([1,2,3],[4,5,6],[7,8,9],[4,5,6])
 
fn FindItemByZ arr val = (
	local tmp = for i=1 to arr.count where \
		arr[i][3] == val collect i
	if arr.count == 0 then 0 else tmp[1]
)
-- example usage:
itmIndex = FindItemByZ _array 6

May try this one too. It s'd be more faster, i think.

fn FindItemByZ arr val = (
	local idx = 0, run = on
	for i=1 to arr.count while run do
		if arr[i][3]==val do (idx=i; run=off)
	idx
)
-- example usage:
itmIndex = FindItemByZ _array 6

my recent MAXScripts RSS (archive here)

mjbg's picture

Thank you very much the first

Thank you very much

the first one worked really well

the second it only returned the first occurrence like you said.. but its very good as well

mjbg's picture

i found a workaround _array =

i found a workaround

_array = #([1,2,3],[4,5,6],[7,8,9])
_arrayZ = #()

for i in 1 to _array.count do append _arrayZ _array[i].z

findItem _arrayZ 6

still apreciated if someone know another way to do it
thank you

Comment viewing options

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