PDA

View Full Version : query index number of array item


biorhythm
06-02-2007, 10: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?

thanks:)

grantimus
06-02-2007, 11: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[]){
for($i=0;$i<size($array);$i++)
if($item == $array[$i])
return($i);
return(-1); //if item is not in array then -1, and invalid array index, is returned
}

//findInStringArray("hello",{"joe","bob","frank","hello","juan","jose","hola"});
// 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.

biorhythm
06-03-2007, 03:47 PM
works a charm, thanks grantimus :)

ewerybody
06-04-2007, 09:26 AM
funny. The same question was posted on highend3D just some days ago: :D
http://www.highend3d.com/boards/index.php?showtopic=237357


@grantimus:

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 )

biorhythm
06-04-2007, 09: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:)

ewerybody
06-04-2007, 10:57 AM
unlikely.
You see its not impossible though ;]

CGTalk Moderation
06-04-2007, 10: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.