inpace 10 Posted January 16, 2011 Hello everyone, sorry but I have an poor English language. question, if I know a variable how do I know its location in an array? example array = [var1, var2, var3, var4, var5] I know the name var2, how I know your position? (1) Share this post Link to post Share on other sites
gossamersolid 155 Posted January 16, 2011 _posVar2 = getPos var2; ? Share this post Link to post Share on other sites
Andy455 11 Posted January 16, 2011 (edited) I think he is trying to find which element it is in the array rather than the actual world position. In this case you can use the find command: _number = array find var2; // Will return 1 (-1 if not found) Edited January 16, 2011 by Andy455 Share this post Link to post Share on other sites
shuko 59 Posted January 16, 2011 "in an array" So, something like this: pos = array find var2; Share this post Link to post Share on other sites
inpace 10 Posted January 16, 2011 thank you all for the answers, yes i know the commands in and find, but it release a value if the variable is present, but if I dont know the position of this variable but only the name, how i can know his position on the array Share this post Link to post Share on other sites
Bon 12 Posted January 16, 2011 Afaik there's no command for getting the index of an element in an array. But it shouldn't be of any challenge to get it. Without having it tested, I claim this to work: _index = -1; for "_i" from 0 to (count _array - 1) do { if(_array select _i == _element) exitWith{_index = _i}; }; _array is your array and _element is the element you want to get the index of. At the end _index will be either the index of _element in _array, or -1 (i.e. if _element is not in the _array). Share this post Link to post Share on other sites
inpace 10 Posted January 16, 2011 great! very thank's Bon! Share this post Link to post Share on other sites
xeno 234 Posted January 16, 2011 Afaik there's no command for getting the index of an element in an array. find returns the 0 based array index of an element just fine. Xeno Share this post Link to post Share on other sites
inpace 10 Posted January 16, 2011 yes but find work only with string, it is right? I try it with variable dont work!! Share this post Link to post Share on other sites
AZCoder 921 Posted January 16, 2011 The variable in the array needs to have a value (at least from testing) or it returns scalar. For example, this works: _obj1 = 10; _obj2 = 20; _obj3 = 0; _array = [_obj1,_obj2,_obj3]; _pos = _array find _obj2; hint format["position: %1", _pos]; Also tested on editor object names in an array, works fine. Of course it returns the 0-based index, so _obj2 will return a 1. Share this post Link to post Share on other sites
inpace 10 Posted January 17, 2011 ok i tested it, and it works fine, very well thanks to all Share this post Link to post Share on other sites