# How does an array in the form array[x*y] work?



## FriFlo (Apr 17, 2017)

In the KSP manual, I find the definition of an array with two dimension. But it is actually not explained how to recall or write values from/to it.
So, does this mean, the first numbers is columns, the second rows? That's is how I would assume it would work: if you have an array [2*3], to recall the positions or write them to the array, you would define the row (0 or 1) and then the column (0, 1 or 2). So, the first position would be [0*0], the second [1*0], ..., the sixths value [1*2].
But it actually doesn't seem to work that way ... I assume, the multiplication is just a way of saying an array with 6 positions. Then, if you are thinking in terms of rows and columns, to recall a position with columns x and row y you would actually recall array[x*y+y]. 
Which is correct or does it work differently?


----------



## d.healey (Apr 17, 2017)

You don't need to do any maths if you're using the Sublime Text compiler - https://github.com/nojanath/SublimeKSP/wiki/Added-Features#multidimensional-arrays


----------



## Darius (Apr 17, 2017)

That's nearly right;
for a 2 dimensional array of size [X, Y] or [X*Y], an index can be defined as [(x*X)+y]

This can be extended further to a 3 dimensional array [X, Y, Z] sized [X*Y*Z], indexed by [(z*ZX)+(x*X)+y]
or 4 dimensions [(w*WZX)+(z*ZX)+(x*X)+y]
etc...


----------



## FriFlo (Apr 17, 2017)

Thanks, guys! So, the [x*y] is rather a multiplication, just to define the size of the array, right? I could just as well define an array[6] and it would work the same way as if I would declare an array[2*3]? The two dimensions just serve as a reminder of the way I want to store values there. I hope, I got that right, this time.
Thanks for the sublime link. I am already using sublime but that link was very helpful! Instead of that, I still think it is important to understand the code, as it gets compiled, so, that is why I want to get, how it is being processed.


----------



## neblix (Apr 18, 2017)

It's just a multiplication to create the size.

I can say I want to create an array 88 notes by 127 velocities

declare %array[88*127]

is the same exact thing as

declare %array[11176]

but more readable and clear showing purpose (88 groups of 127).

But really, if you have sublime, you should be doing

declare %array[88, 127]

so you can access like %array[0, 1] which is the 1st column in the 0th row (think of it like coordinates) or "note 0 velocity 1" in the example.


----------



## FriFlo (Apr 22, 2017)

neblix said:


> But really, if you have sublime, you should be doing
> 
> declare %array[88, 127]
> 
> so you can access like %array[0, 1] which is the 1st column in the 0th row (think of it like coordinates) or "note 0 velocity 1" in the example.


Yeah, I got that and I suppose I will start doing it. Just wanted to understand how the compiled script actually works.


----------



## d.healey (Apr 22, 2017)

FriFlo said:


> Yeah, I got that and I suppose I will start doing it. Just wanted to understand how the compiled script actually works.


This will probably be of interest to you if you haven't seen it: http://www.nilsliberg.se/ksp/scripts/tutorial/editor.html#property


----------

