View Full Version : query index number of array item

06 June 2007, 09:15 PM
is there a way to search an array to find if an item exists in it, and then find the index position of the item?

i found the stringArrayContains command to work in finding if the item exists in an array but as to finding its index im not so sure.

any ideas?


06 June 2007, 10:00 PM
As far as I know, Maya has no built in tool to do this. But it isn't hard to write a proc that will do what you want.

global proc int findInStringArray(string $item, string $array[]){
if($item == $array[$i])
return(-1); //if item is not in array then -1, and invalid array index, is returned

// Result: 3 //

Keep in mind that if there is more than one instance of $item in $array the above proc will only return the first instance it finds, all the rest will be ignored.

06 June 2007, 02:47 PM
works a charm, thanks grantimus :)

06 June 2007, 08:26 AM
funny. The same question was posted on highend3D just some days ago: :D


I found that for($i=0;$i<size($array);$i++) calls size($array) on every loop!! So getting size before and using a variable might be faster when you really want high performance. ( >1000 loops )

06 June 2007, 08:44 AM
cool, so i gues im not the only one wanting this function, maby maya 9 will have some new mel commands?

thanks for the links ewerybody:)

06 June 2007, 09:57 AM
You see its not impossible though ;]

CGTalk Moderation
06 June 2007, 09:57 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.