SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 09/09/2012, 05:43 PM   #1
Kush
Gangsta
 
Join Date: May 2011
Posts: 781
Reputation: 126
Default Arrays question!

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!
__________________
Quote:
Originally Posted by Dripac
i have more reputation than you, that means i am more clever than you my friend
Kush is offline   Reply With Quote
Old 09/09/2012, 05:50 PM   #2
leonardo1434
High-roller
 
leonardo1434's Avatar
 
Join Date: Mar 2012
Posts: 1,667
Reputation: 179
Default Re: Arrays question!

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);
}
__________________
c++
leonardo1434 is offline   Reply With Quote
Old 09/09/2012, 06:02 PM   #3
Larceny
Huge Clucker
 
Larceny's Avatar
 
Join Date: Apr 2006
Posts: 276
Reputation: 228
Default Re: Arrays question!

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.
Larceny is offline   Reply With Quote
Old 09/09/2012, 06:24 PM   #4
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,066
Reputation: 2660
Default Re: Arrays question!

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.
__________________
Vince is offline   Reply With Quote
Old 09/09/2012, 07:10 PM   #5
Finn
High-roller
 
Join Date: Jan 2008
Posts: 1,464
Reputation: 382
Default Re: Arrays question!

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.
Finn is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with arrays lidor5353 Scripting Help 4 25/08/2012 07:54 PM
Question about arrays and tables xtremedeblin Help Archive 0 14/08/2010 02:04 AM
[Question] Arrays ArTisT Help Archive 1 20/06/2010 06:44 PM
Question about arrays Outbreak Help Archive 5 23/04/2009 10:56 PM


All times are GMT. The time now is 12:30 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.