Raised This Month: $51 Target: $400
 12% 

multi-dimensional arrays && structuring arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 18:31   multi-dimensional arrays && structuring arrays
Reply With Quote #1

Hey there,
I have some problems now, I decided to use structs in Pawn, but encounter a problem, Working with arrays in Pawn very strange.
I try to struct my data:

PHP Code:
//Just make some struct:
enum myStruct
{
    
_myString,
    
_myStringArray[4]
};

//Ok, let's try to use it:
new const myArray[][myStruct][128] =
{
    {
        
"Class 1",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    },
    
    {
        
"Class 2",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    }

Quote:
error: 052: multi-dimensional arrays must be fully initialized
Ok, try to fix it
PHP Code:
new const myArray[2][myStruct][128] =
{
    {
        
"Class 1",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    },
    
    {
        
"Class 2",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    }

Quote:
error: 052: multi-dimensional arrays must be fully initialized
Hm, what?

Ok, I have some idea, If we have to use constants and fully initialized we can use "double struct":

PHP Code:
enum myStructSlots
{
    
_slot1[128],
    
_slot2[128],
    
_slot3[128],
    
_slot4[128]
};

enum myStruct
{
    
_myString[128],
    
_myStringArray[myStructSlots]
};

new const 
myArray[2][myStruct] =
{
    {
        
"Class 1",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    },
    
    {
        
"Class 2",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    }
}; 
Quote:
Done
OMG! THANKS! PAWN!

Wait...

PHP Code:
log_amx("[%s]"myArray[0][_myString]);
log_amx("[%s]"myArray[0][_myStringArray][_slot1]);
log_amx("[%s]"myArray[0][_myStringArray][_slot2]);
log_amx("[%s]"myArray[0][_myStringArray][_slot3]);
log_amx("[%s]"myArray[0][_myStringArray][_slot4]);

log_amx("[%s]"myArray[1][_myString]);
log_amx("[%s]"myArray[1][_myStringArray][_slot1]);
log_amx("[%s]"myArray[1][_myStringArray][_slot2]);
log_amx("[%s]"myArray[1][_myStringArray][_slot3]);
log_amx("[%s]"myArray[1][_myStringArray][_slot4]); 
Quote:
[Class 1]
[Some data 1]
[]
[]
[]
[Class 2]
[Some data 1]
[]
[]
[]
Something bad... I think... "Some data 1" has 11 charmax, just try:
PHP Code:
log_amx("[%s]"myArray[0][_myString]);
log_amx("[%s]"myArray[0][_myStringArray][_slot1]);
log_amx("[%s]"myArray[0][_myStringArray][_slot1][12]); 
Quote:
[Class 1]
[Some data 1]
[Some data 2]
Try to fix:
PHP Code:
enum myStructSlots
{
    
_slot1,
    
_slot2,
    
_slot3,
    
_slot4
};

enum myStruct
{
    
_myString,
    
_myStringArray[myStructSlots]
};

new const 
myArray[2][myStruct][128] =
{
    {
        
"Class 1",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    },
    
    {
        
"Class 2",
        
        {
            
"Some data 1",
            
"Some data 2",
            
"Some data 3",
            
"Some data 4"
        
}
    }
}; 
Quote:
error: 052: multi-dimensional arrays must be fully initialized
Maybe multidimensional array so multidimensional? Try to use few arrays:

PHP Code:
enum myStructSlots
{
    
_slot1,
    
_slot2,
    
_slot3,
    
_slot4
};

enum myStruct
{
    
_myString,
    
_myStringArray[myStructSlots]
};

new const 
myArray1[myStruct][128] =
{
    
"Class 1",
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
Quote:
error: 052: multi-dimensional arrays must be fully initialized
Ok.

PHP Code:
enum myStructSlots
{
    
_slot1[128],
    
_slot2[128],
    
_slot3[128],
    
_slot4[128]
};

enum myStruct
{
    
_myString[128],
    
_myStringArray[myStructSlots]
};

new const 
myArray1[myStruct] =
{
    
"Class 1",
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
Quote:
Done
PHP Code:
log_amx("[%s]"myArray1[_myString]);
log_amx("[%s]"myArray1[_myStringArray][_slot1]);
log_amx("[%s]"myArray1[_myStringArray][_slot1][12]);
log_amx("[%s]"myArray1[_myStringArray][_slot2]); 
Quote:
[Class 1]
[Some data 1]
[Some data 2]
[]
Arrrrrrrrrrrrrrr...

Please. Help me...
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 19:17.
Hedgehog Fog is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 18:44   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #2

I don't need slots for first value, but maybe it's fix?

PHP Code:
enum myStruct
{
    
_myString[myStructSlots],
    
_myStringArray[myStructSlots]
};

new const 
myArray1[myStruct] =
{
    {
        
"Class 1",
        
"NULL",
        
"NULL",
        
"NULL"
    
},
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
PHP Code:
log_amx("[%s]"myArray1[_myString][_slot1]);
log_amx("[%s]"myArray1[_myStringArray][_slot1]);
log_amx("[%s]"myArray1[_myStringArray][_slot1][13]);
log_amx("[%s]"myArray1[_myStringArray][_slot2]); 
Quote:
error: 052: multi-dimensional arrays must be fully initialized
Nope...
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 19:09.
Hedgehog Fog is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 18:51   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #3

Oh, I fixed it!

PHP Code:
enum myStruct
{
    
_myString,
    
_myStringArray
};

new const 
myArray1[myStruct][4][128] =
{
    {
        
"Class 1"
        
""
        
""
        
""
    
},
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
PHP Code:
log_amx("[%s]"myArray1[_myString][0]);
log_amx("[%s]"myArray1[_myStringArray][0]);
log_amx("[%s]"myArray1[_myStringArray][0][13]);
log_amx("[%s]"myArray1[_myStringArray][1]); 
Quote:
[Class 1]
[Some data 1]
[]
[Some data 2]
...but first values are empty, try this:
PHP Code:
new const myArray1[myStruct][][128] =
{
    {
        
"Class 1"
    
},
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
Quote:
error 018 initialization data exceeds declared size
If I initialize first array of array like:
PHP Code:
{
 
"Class 1"""""""

I have only 4 slots for next arrays of array.

Time to use enum again:
PHP Code:
enum myStruct
{
    
_myString,
    
_myStringArray[4]
};

new const 
myArray1[myStruct][128] =
{
    
"Class 1",
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
Quote:
error: 052: multi-dimensional arrays must be fully initialized
Any ideas?
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 19:15.
Hedgehog Fog is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-19-2015 , 19:52   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #4

You should never have a dimension declared after your use your 'myStruct' enum. Also, strings in Pawn are arrays of characters. I'm guessing it should be like this:

Code:
enum myStruct
{
    _myString,
    _myStringArray[4][32]
};

new const myArray1[myStruct] =
{
    "Class 1",
    
    {
        "Some data 1",
        "Some data 2",
        "Some data 3",
        "Some data 4"
    }
};
__________________

Last edited by fysiks; 03-19-2015 at 19:54.
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-19-2015 , 19:54   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #5

PHP Code:
enum myStruct
{
    
_myClass],
    
_myString1],
    
_myString2],
    
_myString3]
};

new const 
myArrays][ myStruct ] =
{
    {
        
"Class1",
        
"abc1" 
        
"def1" 
        
"ghi1",
    },
    {
        
"Class2",
        
"abc2"
        
"def2"
        
"ghi2",
    },
    {
        
"Class3",
        
"abc3"
        
"def3"
        
"ghi3"
    
}
}; 
__________________
Bugsy is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 20:00   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #6

fysiks,
True, but enum can't contain multi-dimensional arrays. Look at 3 code version at first post.

Bugsy,
I need another struct with path like this:
PHP Code:
classID/classname/chars 
PHP Code:
classID/classdata/dataID/chars 
Quote:
PHP Code:
new const myArrays][ myStruct 
Can be dynamic:
PHP Code:
new const myArrays[][myStruct
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 20:03.
Hedgehog Fog is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-19-2015 , 20:18   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #7

Quote:
Originally Posted by Hedgehog95 View Post
Bugsy,
I need another struct with path like this:
PHP Code:
classID/classname/chars 
PHP Code:
classID/classdata/dataID/chars 
I showed you how this can be done. Just follow the same format.

Quote:
Originally Posted by Hedgehog95 View Post
Can be dynamic:
PHP Code:
new const myArrays[][myStruct
Yes, I know.
__________________
Bugsy is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 20:29   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
I showed you how this can be done. Just follow the same format.
This isn't what I need, actually.
I know about this method, it is simple. I need a more advanced structure, with path, like path in my last post.

It should be a resource class, like:
PHP Code:
new const myClass[myStruct][128]
{
    
"classname",
    
    
//values for data 1
    
{
        
"data1_1",
        
"data1_2",
        
"data1_3",
        
"data1_4"
    
},
    
    
//values for data 2
    
{
        
"data2_1",
        
"data2_2",
        
"data2_3",
        
"data2_4"
    
}

where data will be used as:

PHP Code:
new sizeData1 sizeof myClass[_myStringArray1];
new 
sizeData2 sizeof myClass[_myStringArray2];

new 
classname[] = myClass[_myStringArray1][_myString];
new 
data1[] = myClass[_myStringArray1][random(sizeData1--)];
new 
data2[] = myClass[_myStringArray2][random(sizeData2--)]; 
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 20:34.
Hedgehog Fog is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-19-2015 , 20:48   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #9

Pawn is a pretty basic language, I'm not sure if what you want is doable. Possibly with macros/hacks.
__________________
Bugsy is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 03-19-2015 , 21:02   Re: multi-dimensional arrays && structuring arrays
Reply With Quote #10

PHP Code:
enum myStruct
{
    
_myString,
    
_myStringArray
};

new const 
myArray1[myStruct][4][128] =
{
    {
        
"Class 1"
        
""
        
""
        
""
    
},
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
This is a good way, but initialize an empty slots not the best idea.

PHP Code:
enum myStruct
{
    
_myString,
    
_myStringArray[4]
};

new const 
myArray1[myStruct][128] =
{
    
"Class 1",
    
    {
        
"Some data 1",
        
"Some data 2",
        
"Some data 3",
        
"Some data 4"
    
}
}; 
I don't know why it doesn't work and why I got error:
Quote:
error: 052: multi-dimensional arrays must be fully initialized
May be there is a way to optimize the code of the first version and dont use this if I have 20 slots:
Quote:
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 03-19-2015 at 21:05.
Hedgehog Fog is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 17:39.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode