Jump to content
Sign in to follow this  
ZNorQ

Checking the content of an Array?

Recommended Posts

By using 'onFlare.sqs' (seems onFlare.sqf isn't supported), I'm trying to create a variable containing the real color name of the flare.

Since it's sqs, this is the following formula I'd like to use; (Just a lame example)

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_flareColor = _this select 0

?(_flareColor == [1,0,0]) : hint "The flare is red"

?(_flareColor == [1,1,0]) : hint "The flare is yellow"

.. etc. for white and green ...

This doesn't work as seems I can't check the content of an array this way, so I had to actually check the content of each array cell like this;

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_flareColorRed = _this select 0 select 0

_flareColorGreen = _this select 0 select 1

_flareColorBlue = _this select 0 select 2

?(_FlareColorRed == 1) and (_FlareColorGreen == 0) and (_FlareColorBlue == 0) : hint "The flare is red"

... etc..

Is this the only way to do this, or is there a better way.

Just to clarify; The question is what is the best way to check the content of an array?

ZNorQ

Share this post


Link to post
Share on other sites

For not too big arrays, you could use the format command to help you compare an array to a string or to another array.

Example condition taken from my buildingPosCount function (can be found from www.ofpec.com):

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">format ["%1", _this buildingPos _i] != "[0,0,0]"

The _this buildingPos _i returns a position array, which is then converted into a string with format and then compared against an array [0,0,0] which is already in string form. You could construct both sides of the condition statement with format if needed.

Note: at least in OFP, if the array was too big, OFP would crash to desktop. But for small arrays like in the example I gave, its very easy and short way to compare arrays.

Share this post


Link to post
Share on other sites

Ok, so the answer in my case should then be;

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_flareColor = format["%1", _this select 0]

?(_flareColor == "[1,0,0]") : hint "The flare is red"

I'll give it a try...

ZNorQ

Share this post


Link to post
Share on other sites

And it works! Makes it abit cleaner atleast.

But, it should'a been possible to do a test like this; ? (_MyArra == [1,0,0,"test"]) : <some code>

.. Anyway, thanks Baddo!

ZNorQ

Share this post


Link to post
Share on other sites

Here, you can use this function to compare any 2 arrays. (eg: [1.5, 2.6, 3.7])

It doesn't handle nested arrays though (eg: [1, 2, [3,4]] ). I had it checking it recursively, but didn't have time to test it, so didn't include that part.

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

{

private ["_Array1", "_Array2", "_Result"];

_Array1 = _this select 0;

_Array2 = _this select 1;

_Result = true;

if (count _Array1 != count _Array2) then

{

_Result = false

}

else

{

private ["_i"];

for "_i" from 0 to (count _Array1 - 1) do

{

if (typeName (_Array1 select _i) == "ARRAY" || typeName (_Array2 select _i) == "ARRAY") then

{

_Result = false // it doesn't compare nested arrays yet

}

else

{

if (_Array1 select _i != _Array2 select _i) then

{

_Result = false

};

};

};

};

_Result;

};

Then just call:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_a = [1.5, 2.6, 3.7];

_b = [6.6, 2.6, 3.7];

if ([_a, _b] call fn_ArraysAreEqual) then { bla... };

Share this post


Link to post
Share on other sites

String-comparison is slow -- generally speaking, and not only in BIS' products. For such tiny strings, however, this might not really be an argument. I'd go for the suggested function, nevertheless.

Share this post


Link to post
Share on other sites

Well I think the number of code lines you need to write in order to accomplish something has quite a lot of significance. In other words, the simpler the better.

It would be nice to see actual benchmark results by the way. Might do that myself but I don't promise anything.

Share this post


Link to post
Share on other sites

The number of ad hoc visible code-lines does not necessarily mean that the code de facto is simple. Hence, you should take a look at what's behind format, which is nothing but another function "hiding" code -- or rather executing code/things for you. So, the array-comparison function, when called/evoked, will result in "simple" code (per your definition) as well.

However, as I said before, for the current "problem", the string-comparison is absolutely okay. The posted function remains a more generic solution, though.

[Edit] Syntax

Share this post


Link to post
Share on other sites

@Dr Eyeball; I'll implemend your code into my missions - if you don't mind. I've put your nick and a link to this topic as a reference in the code.

As for my example, that seems straight forward, but I'm sure that I'm going to match more complex arrays as I really get into the mission making. If this code is the solution, then I bow my head deep into the dust, for you Dr! smile_o.gif

I just find it strange that we have to create such functions just to compare some arrays! This should have been in place from day one, don't anyone agree?

ZNorQ

PS! Thanks for the feedback, guys, you've all been helpful. And; If you do the benchmark tests, give me a heads up? smile_o.gif

Share this post


Link to post
Share on other sites
I just find it strange that we have to create such functions just to compare some arrays! This should have been in place from day one, don't anyone agree?

Yes I agree. Well that would be a "convenience" function, as you can, with the currently existing functions, accomplish the same task, so BIS decided they will not make such "convenience" function for you but rather let you write it yourself.

weedomatic, yes I know what you said can be true. I also say again smile_o.gif that benchmark results would be nice to see as we, in my humble opinion, can not say which method is more efficient if we haven't benchmarked both. I will refuse to guess such things.

And about what is "simple" code. When we go to a higher-level language, the language is supposed to do more for us and reduce the amount of code we need to write by ourselfs. Using format to compare arrays is one such case where the amount of code we write can be reduced (let others write the code and maintain it for us).

Share this post


Link to post
Share on other sites
Quote[/b] ]This should have been in place from day one, don't anyone agree?

Can you draw a comparison? I'm trying to think of a programming language that lets you compare mixed content like pointers and such?

I haven’t done any windows programming for quite some time. So perhaps I'm missing something?

Share this post


Link to post
Share on other sites
Quote[/b] ]This should have been in place from day one, don't anyone agree?

Can you draw a comparison? I'm trying to think of a programming language that lets you compare mixed content like pointers and such?

I haven’t done any windows programming for quite some time. So perhaps I'm missing something?

I'm talking about testing the content of an array variable, ie. _myArray == ["TEXT", 5, [1,2]]..

Btw, do pointers exist in ArmA?

ZNorQ.

Share this post


Link to post
Share on other sites
Btw, do pointers exist in ArmA?

Yes actually.

When you do something like this:

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

_myArray = [0,1,2,3];

_myPointerTo_myArray = _myArray;

_myPointerTo_myArray is updated with changes to _myArray and vice versa so long as it's done correctly. You can't modify the arrays dimensions (I think, not positive there) and you must change its values with the set command.

A solid example that's actually done pretty commonly is passing the list of a trigger to a script. The list is actually an array and it's contents are refreshed in the script as they change.

Of course there's a hitch (there always is in arma). When you save and load the game, the pointer is lost. This means you have to use one of the following options:

1: Don't use it.

2: Use it only in MP and only where global array tracking isn't an issue.

3: Rig up a test on something that also resets with a loadgame (like time) and in the event of a lost pointer, terminate and restart the script which may or may not be practical.

Share this post


Link to post
Share on other sites

my short (and bit faster) array compare solution:

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

//Check for Arrays are equal by Zaphod (berzerk lab.) 2007

fn_ArraysAreEqual={

if(count (_this select 0)!=count (_this select 0))exitwith{false};

for[{_i=0},{_i<count (_this select 0)},{_i=_i+1}]do{

if(((_this select 0) select _i)!=((_this select 1) select _i))exitwith{false};

};

true

};

just call it the same way Dr. Eyeball did, since the main function body is quite the same ([_a,_b] call fn_ArraysAreEqual) ...

Regards,

Zap

Share this post


Link to post
Share on other sites
my short (and bit faster) array compare solution:

Benchmark results? xmas_o.gif

Share this post


Link to post
Share on other sites
Quote[/b] ]

The question is what is the best way to check the content of an array?

this is what i do

_array = [1,2,3,4,5,6,7,8,9];

titletext[format ["\n\n\n %1 ", _array], "Plain down"];

then the array will show on your screen so you can see what is happening

Share this post


Link to post
Share on other sites

I dare to say that is not what he was after, Zonekiller.

Yes, if you want to check what is in the array, then that can be used.

But if it is the game which needs to check what is in the array, then that is not what can be used.

Share this post


Link to post
Share on other sites
my short (and bit faster) array compare solution:

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

//Check for Arrays are equal by Zaphod (berzerk lab.) 2007

fn_ArraysAreEqual={

if(count (_this select 0)!=count (_this select 0))exitwith{false};

for[{_i=0},{_i<count (_this select 0)},{_i=_i+1}]do{

if(((_this select 0) select _i)!=((_this select 1) select _i))exitwith{false};

};

true

};

just call it the same way Dr. Eyeball did, since the main function body is quite the same ([_a,_b] call fn_ArraysAreEqual) ...

Regards,

Zap

vf_arraysareequal=

{

private ["_i, _array"];

_array=_this select 1;

if(count _array!=count (_this select 0)) exitwith{false};

_i=-1;

({_i=_i+1; _x != (_array select _i)} count (_this select 0))==0

};

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  

×