Jump to content
Sign in to follow this  
juddo

removing a value from a nested array issues

Recommended Posts

hello.

i've read many threads on here and the wiki page, but noone appears to have asked how to remove a value from a 2d array.

im copying an array like this;

_myarray = [];
_myarray = + _copythisarray;

the wiki says to remove an item from a nested array;

_array = [["first","hello1"],["second","hello2"],["third","hello3"]];
_array set [1,-1];
_array = _array - [-1];
// _array will now contain: [["first","hello1"],["third","hello3"]]

i understand the above, but i have an array in an array in an array (2d array). example;

_myarray = [
[ [0,0,0], [1,1,1],  [2,2,2] ],
[ [3,3,3], [4,4,4],  [5,5,5] ],
[ [6,6,6], [7,7,7],  [8,8,8] ]
];

i want to remove 2,2 for example (8,8,8). i tried many different brackets and scenarios like;

_myarray select 2 set [2, "delete_me"];
_myarray select 2 = _myarray select 2 - ["delete_me"];

or

(_myarray select 2) set [2, "delete_me"];
(_myarray select 2) = (_myarray select 2) - ["delete_me"];

i tried a few other things but they didnt work either. i keep getting error messages in game saying missing ';' end of line. which makes me think its related to how i am calling the array...

anyone come across this or know a work around?

any help would be very appreciated!

thanks for your time.

best regards,

justin

Edited by juddo

Share this post


Link to post
Share on other sites

without worrying about the intricacies of array manipulation, which i'll assume you probably did right or will get right, at a glance..

it looks like you have one too many close brackets (]) at the end of your '_myarray = ...' statement? hehe

Share this post


Link to post
Share on other sites

thanks. you are correct :)

i edited it and checked i wasnt doing this with my code (which i am not). i typed that array off the top of my head...

i edited my first post to reflect what i have in my script.

Share this post


Link to post
Share on other sites

private ["_myArray","_myTempArray"];
_myArray = [
[ 
	[0,0,0], 
	[1,1,1],  
	[2,2,2] 
],[ 
	[3,3,3], 
	[4,4,4],  
	[5,5,5] 
],[ 
	[6,6,6], 
	[7,7,7],  
	[8,8,8] 
]
];
_myTempArray = _myArray select 2;
_myTempArray set [2, "x"];
_myTempArray = _myTempArray - ["x"];
_myArray set [2, _myTempArray];

you have to go deeper

Share this post


Link to post
Share on other sites

Does: systemChat (str ([[_myarray select 2] select 2] select 2); display 8?

Share this post


Link to post
Share on other sites

Here's the solution:

You set the array like this:

_myarray = [
[ [0,0,0], [1,1,1],  [2,2,2] ],
[ [3,3,3], [4,4,4],  [5,5,5] ],
[ [6,6,6], [7,7,7],  [8,8,8] ]
];

To delete the last item ([ [6,6,6], [7,7,7], [8,8,8] ]), you do like this:

_myarray set [2, "delete_me"];
_myarray = _myarray - ["delete_me"];

The secret is this: after you have set item 2 to "delete_me", the array will look like this:

[
[ [0,0,0], [1,1,1],  [2,2,2] ],
[ [3,3,3], [4,4,4],  [5,5,5] ],
"delete_me"
]

You did this because you wanted to replace the array at position 2 with a value (since you cannot delete an array by "subtracting" an item that is an array). Now you need to subtract one array from the other, so you subtract an array with one item ("delete_me") from _myarray:

_myarray = _myarray - ["delete_me"];

All array items "delete_me" will now be removed from _myarray.

Edited by Engima

Share this post


Link to post
Share on other sites

The problem with your solution is that you deleted the entire first dimension element when he only wanted to remove [8,8,8] from the second dimension.

Share this post


Link to post
Share on other sites

Ah, sorry!

What about this then:

_myarray select 2 set [2, "delete_me"];
_myarray set [2, _myarray select 2 - ["delete_me"]];

Share this post


Link to post
Share on other sites
Ah, sorry!

What about this then:

_myarray select 2 set [2, "delete_me"];
_myarray set [2, _myarray select 2 - ["delete_me"]];

Editor is your friend. This ^^^ doesnt work.

Share this post


Link to post
Share on other sites
Editor is your friend. This ^^^ doesnt work.

And what does it say? Tried with ()s? Tried a middle step with a temp array? Principle should work.

Share this post


Link to post
Share on other sites

Have you tried the BIS funcs? BIS_fnc_removeNestedElement , see the in editor functions list under Arrays.

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  

×