Jump to content
Sign in to follow this  
scottb613

Proper way to append to an array ?

Recommended Posts

Hi Folks,

 

For today's question for those who have the patience - LOL - I truly search for the answers and make many attempts with the code before posting...

 

What's the proper way to append to an array ? The code below is simply testing for the existence of a "marker" - and if it exists - I want an array of the marker names... I've tried many different iterations trying to get this to work... At one point I was building an array of "position arrays" but var array was losing the position reference - so three positions were giving me a "count" of 9 reflecting each element as opposed to the position arrays  - should give me 3... Now I can't even get past the "count" command as it appears to hang the code - I am assuming because my array is mangled... Can someone point me in the right direction ???

 

The wiki stated that an array destroys the original array when appending elements so I am not sure if I can use the same array in the definition when appending - such as: _myArray = _myArray + [newElement1,newElement2,newElement3];

 

Thanks...

_m1 = getMarkerPos "m1";
_m2 = getMarkerPos "m2";
_m3 = getMarkerPos "m3";
_m4 = getMarkerPos "m4";
_m5 = getMarkerPos "m5";
_m6 = getMarkerPos "m6";

_mkrnam1 = "m1";
_mkrnam2 = "m2";
_mkrnam3 = "m3";
_mkrnam4 = "m4";
_mkrnam5 = "m5";
_mkrnam6 = "m6";
_cnt = 1;
{
	_arrvar = call compile format ["_m%1",_cnt];
	_mkXpam = _arrvar select 0;
	_mkYpam = _arrvar select 1;
	
	if (_mkXpam > 0 || _mkYpam > 0) then
	{
		if (_cnt == 1) then
		{
			_mkrarr = _x;
		}
		else
		{
			// example: _myArray = _myArray + [newElement1,newElement2,newElement3];
			_mkrarr1 = _mkrarr;
			_mkrarr2 = _x;
			_mkrarr = _mkrarr1 + _mkrarr2;
		};
		_mkrcnt = count _mkrarr;
		_cnt = _cnt + 1;
	};
} foreach [_mkrnam1,_mkrnam2,_mkrnam3,_mkrnam4,_mkrnam5,_mkrnam6];

Regards,

Scott

Share this post


Link to post
Share on other sites

pushBack adds to array.

 

You could get the existing markers like this:

_markerNames = ["m1","m2","m3","m4","m5","m6"];
_existingMarkers = [];

{  if !(getmarkerpos _x isEqualto [0,0,0]) then {
    _existingMarks pushback _x    }
    } foreach _markerNames;

or simply :

_existingMarkers = ["m1","m2","m3","m4","m5","m6"] select { !(getMarkerPos _x isEqualto [0,0,0]) };
  • Like 3

Share this post


Link to post
Share on other sites


_existingMarkers = [];

_arrayofstrings = ["m1","m2","m3","m4","m5","m6"];

{

    if (_x in _arrayofstrings) then {

        _existingMarkers pushback _x;

    };

} forEach allMapMarkers;

_existingMarkers

  • Like 1

Share this post


Link to post
Share on other sites

Hi G,

 

LOL and here we see the difference between a real coder and a hack - everything I've been struggling with condensed into a single line of code... Wow - thanks...

 

So empty arrays need to be declared like this: _existingMarkers = []; 

 

Does this do anything different than just trying to populate a var with an array element ?

 

Thanks...

 

Regards,
Scott

Share this post


Link to post
Share on other sites

So empty arrays need to be declared like this: _existingMarkers = []; 

 

Does this do anything different than just trying to populate a var with an array element ?

pushBack just needs an existing array to push to. It can't create a new array.
  • Like 1

Share this post


Link to post
Share on other sites

Hi Folks,

Thank you both for your responses - gives me plenty to play with - have a good day...

Regards,

Scott

Sent from my iPad using Tapatalk

Share this post


Link to post
Share on other sites

 

LOL and here we see the difference between a real coder and a hack - everything I've been struggling with condensed into a single line of code... Wow - thanks...

 

Thanks...

 

Regards,

Scott

 

I think you dont know the difference between hacker/coder and "spaguetti" code.

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  

×