2 dimensional array declaration
#1

in lua you should do something like this:

pawn Код:
local pack = {
[1] = {"weapon 1", "weapon2", "weapon 3"},
[2] = {"weapon 4", "weapon5", "weapon 6"}
};
is it possible to do in pawn too?

basically i'm creating 2 dimensional array and right after that I'm inserting values, so the array should look like this

pawn Код:
//output
pack[0][0] = "weapon 1"
pack[1][1] = "weapon5"
Reply
#2

pawn Код:
new TwoDimensionalArray[2][3][16] =
{
    {"weapon 1", "weapon 2", "weapon 3"},
    {"weapon 4", "weapon 5", "weapon 6"}
};
Because you're using strings, this will be actually called three dimensional array.
2 is the amount of "lines", 3 is the amount of "columns" (or how many cells will be in each row/line), 16 is the maximum length for each inserted string.

You can also use [] instead of the [2] so you'll not be have to count the lines everytime.

To edit an existing value:
pawn Код:
TwoDimensionalArray[0][2] = "weapon 9";
// weapon 3 -> weapon 9
For a two dimensional array of numbers, you'll use
pawn Код:
new Nums[2][3] =
{
    {2,3,2},
    {5,5,3}
};
Reply
#3

Quote:
Originally Posted by Amit_B
Посмотреть сообщение
pawn Код:
new TwoDimensionalArray[2][3][16] =
{
    {"weapon 1", "weapon 2", "weapon 3"},
    {"weapon 4", "weapon 5", "weapon 6"}
};
Because you're using strings, this will be actually called three dimensional array.
2 is the amount of "lines", 3 is the amount of "columns" (or how many cells will be in each row/line), 16 is the maximum length for each inserted string.

You can also use [] instead of the [2] so you'll not be have to count the lines everytime.

To edit an existing value:
pawn Код:
TwoDimensionalArray[0][2] = "weapon 9";
// weapon 3 -> weapon 9
For a two dimensional array of numbers, you'll use
pawn Код:
new Nums[2][3] =
{
    {2,3,2},
    {5,5,3}
};
I actually know how does arrays works, I'm programming for few years already, but in lua you don't have strings or integers, you jsut have variables and you can store everything there

Код:
local a = 1
local b = "a"
local c = "1a"
// everything is valid
and I've never ever created multidimensional arrays in c++ before, so thanks

edit: actually I'm looking into some of my c++ apps and I just noticed that I was using multidimensional array, so for ppl that maybe will encounter this problem in the future, you can declare multidimensional array as this (in c++ array dimensions must have bounds except the first one)

pawn Код:
//c++
int a[][3] =
{
{1,2,3},
{4,5,6}
};

//pawn
new a[][3] =
{
{1,2,3},
{4,5,6}
};

//however in pawn you CAN do this

new a[][] =
{
{1,2,3},
{4,5,6}
};
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)