Jump to content
Sign in to follow this  
inpace

How i know the position of a variable in one array?

Recommended Posts

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

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 by Andy455

Share this post


Link to post
Share on other sites

"in an array"

So, something like this:

pos = array find var2;

Share this post


Link to post
Share on other sites

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

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
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

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

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

ok i tested it, and it works fine, very well thanks to all

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×