SA-MP Forums Archive
Arrays question! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Arrays question! (/showthread.php?tid=376280)



Arrays question! - Kush - 09.09.2012

pawn Code:
new hello[2][4];
//is the same as...
new hello[8];
//2*4 = 8
but is...

pawn Code:
new hello[1][1];
//the same as?
new hello[2];
//1*1 = 1...
Sorry was just taking a look at some stuff and just decided to ask.

- Not sure why I placed this thread here but my apologies! Move if necessary!


Re: Arrays question! - leonardo1434 - 09.09.2012

try compile that you will notice the difference.
pawn Code:
new hello[1][1] = {"x"};
new hello2[2] = "x";
public OnFilterScriptInit()
{
   printf(#%s - %s,hello,hello2);
}



Re: Arrays question! - Larceny - 09.09.2012

This is called Multidimensional Array, it is something like, an array inside another array.

pawn Code:
new Array[3][5];
Above you can see a two-dimensional array. In a two-dimensional array, it is convenient to think of the first subscript as being the row, and the 2nd subscript as being the column. Example:

Code:
[0][0]  [0][1]  [0][2]  [0][3]  [0][4]
[1][0]  [1][1]  [1][2]  [1][3]  [1][4]
[2][0]  [2][1]  [2][2]  [2][3]  [2][4]
It is like you have 15 different arrays.

Example of use:

pawn Code:
new Array[3][5];

Array[0][0] = 1;
Array[1][0] = 3;
Array[2][4] = 9;
Array[1][2] = 3;

if(Array[0][0] == Array[1][0])//This returns false!
{
    //...Something
}

if(Array[1][0] == Array[1][2])//But this returns true!
{
    //...Something
}
Hope i could clarify a bit.


Re: Arrays question! - Vince - 09.09.2012

You should picture a single dimensional array as a column of data, a two dimensional array as a table and a three dimensional array as multiple tables stacked on each other.


Re: Arrays question! - Finn - 09.09.2012

I think the easiest way to think of them is to think of them as pockets.

pawn Code:
new arr[2];
You have 2 pockets where you can store 1 number in each.

pawn Code:
new arr[2][2];
You have 2 pockets where you can store 2 numbers in each.

pawn Code:
new arr[5][10];
You have 5 pockets where you can store 10 numbers in each.


If you want to put numbers in those pockets when declaring the array, simply do this:
pawn Code:
new arr[5][10] =
{
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // 1. pocket with 10 numbers
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // 2. pocket with 10 numbers
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // 3. pocket with 10 numbers
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // 4. pocket with 10 numbers
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } // 5. pocket with 10 numbers
};
If you want to edit the array after declaring it, simply do this:
pawn Code:
arr[0][0] = 10;
arr[0][1] = 11;
arr[0][2] = 12;
arr[0][3] = 13;
arr[0][4] = 14;
arr[0][5] = 15;
arr[0][6] = 16;
arr[0][7] = 17;
arr[0][8] = 18;
arr[0][9] = 19;

arr[1][0] = 10;
arr[1][1] = 11;
arr[1][2] = 12;
arr[1][3] = 13;
arr[1][4] = 14;
arr[1][5] = 15;
arr[1][6] = 16;
arr[1][7] = 17;
arr[1][8] = 18;
arr[1][9] = 19;

etc.
Also, wrong section.