Jump to content
Sign in to follow this  
welcome to hell

Coding Question And Answer Thread

Recommended Posts

I have a few questions:

What would be the correct way of replacing elements in an array?

Converting Array to a text string?

Edit:

Nested Arrays?

Edited by Welcome To Hell

Share this post


Link to post
Share on other sites

This was covered pretty well with ArmA, has been around a while. Since the latest ArmA patch you've even been able to publish them across multiplayer clients.

Check out the wiki for the toString and toArray commands.

A nested array is very simple, i.e:

_array1 = [["one_one","one_two"],["one_two","two_two"]];

If I wanted to set "one_two" to "blaa", I could go:

_array2 = _array1 select 1;

_array2 set [1,"blaa"];

_array1 set [0,_array2];

Biki has heaps on this if you're interested in that area.

Share this post


Link to post
Share on other sites

Thank you for the reply it helped a lot, i looked around biki some more, but i have another question.

How would i extract the data position from an array inside the nested array?

For example: here is the nested array:

_array = [ [1,2,3], ["m16","ak","g36"] ];

_myvar =  2;

How would i go about finding which pos (which is "1" in the first array) the var 2 is at?

And if i get the pos? How would i go about querying the second array?

Basically what i really want to know if is there is a simpler way of doing it than described above?

Share this post


Link to post
Share on other sites

if you mean the value 2, then use:

_array = [ [1,2,3], ["m16","ak","g36"] ];

_var = ((_array select 0) select 1);

basically the parenthesis manipulate the order of precedence (what gets evaluated first (innermost first)). So first it grabs the pos (i assume) array which is the 1st element of _array (array counting starts at 0)

[1,2,3]

and then its grabs the 2nd element from that array

2

Share this post


Link to post
Share on other sites

I hope this is correct (latenight coding always gets me):

mk_fArrayToNumber = {
_arr = _this;   
_output = [];

{
	_output set[count _output, _x + 48]    
}forEach _arr;      
parseNumber(toString(_output))  
};

execute the function like so:

_numberValue = [1,2,3] call mk_fArrayToNumber;

Share this post


Link to post
Share on other sites

Is there another function that converts array to string? I played around with toString command and some loops, but i get some weird characters instead.

_array = [1,2,3,4,5]

So the output of the function would be:

_STRarray = "12345";

Share this post


Link to post
Share on other sites

It's because toString uses those numeric values as ascii values, that's why i'm doing a + 48 there.

If you absorb this table, you'll see why.

and to do that thing, just remove the parseNumber function call in the mk_fArrayToNumber function, so it won't be converted to an integer.

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  

×