OFPEC Forum
Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting General => Topic started by: KaRRiLLioN on 28 Jan 2011, 22:44:27
-
I've searched for this, thinking it must already be here but keep getting a zillion results which have nothing to do with it.
I wish to count string elements in an array, for example counting the number of "string1" in the following array.
I've tried countType, but I believe that only works with triggers. Here's sample code from what I've tried unsuccessfully.
_array = ["string1","string2","string1","string3","string1"]
_type = _array select 0
_countName = "_x == _type" count _array
Thanks!
-
Example from BIKI
_found = [1,9,8,3,4,4,4,5,6]; {_x==4} count _found; // returns 3Please note that "_x == _type" in ArmA (2) is just a string, not code. This behaviour changed from OFP.
And it will run in a different scope, so the access to a local variable may not be guaranteed
-
i0n0s beat me to it, you just needed to replace your quotes with {}.
-
In case you get confused with i0n0s's example.
_array = ["string1","string2","string1","string3","string1"]
_type = _array select 0
_countName = {_x == _type} count _array
result will be 3. As there is only one "string1" in the array.
_countName = {typeName _x == "STRING"} count _array;
will result with the total number of strings present in the array.Other types(numbers,objects,etc) will not be counted.
Regards,
haroon1992
-
_array = ["string1","string2","string1","string3","string1"];
_type = _array select 0;
_countName = {_x == _type} count _array;
hint Format[" %1",_countname];
Returns 3 when I tested it and that's how many I see in _array.
-
Thanks guys. I forgot that the quotes changed to brackets back in Arma, so I was still using the OFP method.