Home   Help Search Login Register  

Author Topic: Counting string elements in an array  (Read 2746 times)

0 Members and 1 Guest are viewing this topic.

Offline KaRRiLLioN

  • Members
  • *
  • Grits -n- Peanut Butter are a wonderful thing
    • OFP RTS-3 Real Time War
Counting string elements in an array
« 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.
Quote
_array = ["string1","string2","string1","string3","string1"]
_type = _array select 0
_countName =  "_x == _type" count _array

Thanks!

Offline i0n0s

  • Moderator
  • *****
Re: Counting string elements in an array
« Reply #1 on: 28 Jan 2011, 23:57:22 »
Example from BIKI
Code: [Select]
_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

Offline F2kSel

  • Members
  • *
Re: Counting string elements in an array
« Reply #2 on: 29 Jan 2011, 00:47:33 »
i0n0s beat me to it, you just needed to replace your quotes with {}.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Counting string elements in an array
« Reply #3 on: 29 Jan 2011, 14:06:41 »
In case you get confused with i0n0s's example.
Code: [Select]
_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.

Code: [Select]
_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
« Last Edit: 29 Jan 2011, 16:16:47 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline F2kSel

  • Members
  • *
Re: Counting string elements in an array
« Reply #4 on: 29 Jan 2011, 15:55:49 »

Code: [Select]
_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.

Offline KaRRiLLioN

  • Members
  • *
  • Grits -n- Peanut Butter are a wonderful thing
    • OFP RTS-3 Real Time War
Re: Counting string elements in an array
« Reply #5 on: 31 Jan 2011, 16:36:31 »
Thanks guys.  I forgot that the quotes changed to brackets back in Arma, so I was still using the OFP method.