Jump to content
Sign in to follow this  
charliereddog

Arrays?

Recommended Posts

How is it that you can minus one array from another, but not check whether that array is in the other one?

Share this post


Link to post
Share on other sites

No idea.

But this works:

_var = (str(ar1) == str(ar2));

_var will be true if the arrays match and untrue if the arrays don't match.

(erhm I must admit im unsure if str(xx) works... you might need to test: (format["%1",ar1] == format["%1",ar2]) instead)

Share this post


Link to post
Share on other sites

Thanks. That's actually not going to help my problem one bit, but it's useful to file away for the day it will help.

My issue is

Arr1=[cp1,cp2,cp3,ar1,ar2]

Arr2=[ar3,ar4,ar2,ar1,cp1,cp2,cp3]

Arr1 in Arr2 doesn't work. Bizarely though

Arr2-Arr1 returns [ar3,ar4]. Surely if it can do that, it should be able to work out whether the entries are in the array! whistle.gif

Share this post


Link to post
Share on other sites

so create a function, t_arrayCompare.sqf:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private ["_ar1", "_ar2", "_ok", "_bad"];

_ar1 = [] + (_this select 0);

_ar2 = [] + (_this select 1);

_ok = true; _bad = [];

{

  if (!(_x in _ar2)) then

  {

     _ok = false;

     _bad = _bad + [_x];

  };

} forEach _ar1;

[_ok,_bad]

preCompile it with: <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

t_arrayCompare = compile preProcessFile "t_arrayCompare.sqf";

And use the function e.g as:

_compare = [ [cp1,cp2,cp3,ar1,ar2], [ar3,ar4,ar2,ar1,cp1,cp2,cp3] ] call t_arrayCompare;

_compare variable now contains an array:

[ ARRAYMATCH?, UNMATCHEDITEMS]

Arraymatch will be true if array2 contained all items from array1

Unmatcheditems contains an array with items in _ar1 but not found in _ar2.

You can expand/tweak the function as needed of course, add another loop to turn the test around and capture the differencing objects from both arrays.

Quote[/b] ]Arr1 in Arr2 doesn't work. Bizarely though

Arr2-Arr1 returns [ar3,ar4]. Surely if it can do that, it should be able to work out whether the entries are in the array!  

so add another check on the output (_result in example): <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (count _result==0) then

{

 // array2 contained all items from array1

} else {

 // array2 did not contain all items from array1

};

Share this post


Link to post
Share on other sites

I am not sure of this will help as I only have the most basic of array-related knowledge but it might be something to consider

SPON_deepEquals at ofpec.com

Quote[/b] ]Description:

 Compares any two values, including nil, null, arrays or nested arrays, for equality.

Share this post


Link to post
Share on other sites
SPON_deepEquals at ofpec.com
Quote[/b] ]Description:

 Compares any two values, including nil, null, arrays or nested arrays, for equality.

Good point you have there aswell whistle.gifbiggrin_o.gif

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  

×