Jump to content
Tankbuster

count objects in an array

Recommended Posts

This isn't as simple as the title suggests. I'm fiddling with house class objects being returned by nearobjects and need to make the result array a little more manageable. I'm sure this is computer studies 101, but when I did this in college, we started with 'environment division' and I'm a little rusty :)

 

So, by way of example, how can I get from this;

[tomato, apple, apple, apple, banana, banana, cranberry, tomato, tomato,tomato, apple, pear, pear, apple]

to

[[apple,5], [banana,2], [cranberry,1],[pear,2],[tomato,4]]

Note the output is alpha sorted. I can see that a straightforward alpha sort of the input array is the first step, but how to count them and output like I want?

 

Thanks in advance. :)

Share this post


Link to post
Share on other sites

If they are objects, then based on what value should they be sorted by?

Share this post


Link to post
Share on other sites

I saw that KK, you might notice my example uses fruit too, but I couldn't understand the wiki page. I'm assuming where it says 'duplicities', it means duplicates, which is what I want

Share this post


Link to post
Share on other sites

If they are objects, then based on what value should they be sorted by?

alphabetically sorted by classname

Share this post


Link to post
Share on other sites

I may have been overthinking the biki page and have taken it at its most simplest.

myarray2 =  ["Land_i_House_Big_02_V2_F","Land_i_House_Small_02_V1_F","Land_PowerPoleWooden_L_F","Land_i_Addon_04_V1_F"]

[myarray2] call BIS_fnc_consolidatearray

returned []

Share this post


Link to post
Share on other sites

 

And

myarray2 call BIS_fnc_consolidatearray

Aha! There ya go! That's why they don't call ME prof. :)

Share this post


Link to post
Share on other sites

alphabetically sorted by classname

 

Could have told us that you have an array of strings and not objects (like in your example).  :rolleyes:

Share this post


Link to post
Share on other sites

Could have told us that you have an array of strings and not objects (like in your example).  :rolleyes:

I wasn't sure if was best to work with object names or classnames at the time. I've plumped for classsnames as they are more readable.

Share this post


Link to post
Share on other sites

So, here's the finished (ish) article. Thanks guys!

myarray2 = [];
myarray = player nearobjects  ["House_F", 300];
{myarray2 pushback (typeof _x)} foreach myarray;
myarray3 = myarray2 call BIS_fnc_consolidateArray;
myarray3 sort true;
{diag_log _x}foreach myarray3;

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

×