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

[ TUT ]Dynamic Array


Post New Thread Reply   
 
Thread Tools Display Modes
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-19-2017 , 12:51   Re: [ TUT ]Dynamic Array
Reply With Quote #11

Try to use this in a plugin and compile:
PHP Code:
 new g_newArray[] 
It won't work. Arrays in pawn have a fixed size, which you must specify. Dynamic array allows you to go around this limitation and have unlimited storage space. If you know how much you will store, then likely dynamic arrays are not needed.
You would commonly use them where you don't know how many data will be stored(for example when reading a file with a custom configuration made by user).

Quote:
2. why is this called a 2 Dimensional array if it has got 3 parameters
That's not 2 dimensional, it's 3 dimensional. A bi-dimensional would be new test[34][34]. It's like a matrix.

3.What kind of explanation you want? Anyway, I know I should re-write this, it's old and I don't like how it is right now.
__________________

Last edited by HamletEagle; 01-19-2017 at 12:53.
HamletEagle is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-19-2017 , 14:58   Re: [ TUT ]Dynamic Array
Reply With Quote #12

Quote:
Originally Posted by fysiks View Post
Dynamic arrays are limited to two dimensions, FYI. The first dimension if dynamic and the second is static.
You still can have a dynamic array, of dynamic arrays, where each dynamic array is another dynamic array, and keep going on. This is what I used to destruct my two dimensional arrays. For higher dimensional arrays, the destruction follows the same strategy.
Code:
/**  * General handler to assist object property applying and keep the code clear. This only need  * to be used with destructors/cleaners which does not support uninitialized handlers, requiring  * an if pre-checking.  *  * @param objectHandler           the object handler to be called.  * @param objectIndentifation     the object identification number to be destroyed.  */ #define TRY_TO_APPLY(%1,%2) \ { \     if( %2 ) \     { \         %1( %2 ); \     } \ } /**  * Same as TRY_TO_APPLY(2), but the second argument must to be a two Dimensional Dynamic Array.  *  * @param outerArray                   a Dynamic Array within several Dynamic Arrays.  * @param isToDestroyTheOuterArray     whether to destroy or clear the `outerArray` provided.  */ stock destroy_two_dimensional_array( Array:outerArray, bool:isToDestroyTheOuterArray = true ) {     if( outerArray )     {         new innerArray;         new size = ArraySize( outerArray );         for( new index = 0; index < size; index++ )         {             innerArray = ArrayGetCell( outerArray, index );             TRY_TO_APPLY( ArrayDestroy, Array:innerArray )         }         if( isToDestroyTheOuterArray )         {             TRY_TO_APPLY( ArrayDestroy, outerArray )         }         else         {             TRY_TO_APPLY( ArrayClear, outerArray )         }     } }
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective
addons_zz is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-19-2017 , 15:12   Re: [ TUT ]Dynamic Array
Reply With Quote #13

Dude, do you realize you replied to a post from 2014? Also what fysiks said is true, there are only two dimensions. What you are doing is a workaround, no big discovery.
__________________

Last edited by HamletEagle; 01-19-2017 at 15:14.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-19-2017 , 16:59   Re: [ TUT ]Dynamic Array
Reply With Quote #14

Quote:
Originally Posted by HamletEagle View Post
Try to use this in a plugin and compile:
PHP Code:
 new g_newArray[] 
It won't work. Arrays in pawn have a fixed size, which you must specify. Dynamic array allows you to go around this limitation and have unlimited storage space. If you know how much you will store, then likely dynamic arrays are not needed.
You would commonly use them where you don't know how many data will be stored(for example when reading a file with a custom configuration made by user).


That's not 2 dimensional, it's 3 dimensional. A bi-dimensional would be new test[34][34]. It's like a matrix.

3.What kind of explanation you want? Anyway, I know I should re-write this, it's old and I don't like how it is right now.
Thanks, I'm trying to understand this tutorial.

3. Like a simple explanation, what they are, where to use them..
__________________
edon1337 is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-16-2017 , 13:21   Re: [ TUT ]Dynamic Array
Reply With Quote #15

@hamlet , after 2-3 weeks, I finally understood the purpose of arrays. Thanks so much for the explanation.

Question :
If you set the Array size 1 (ArrayCreate) , how many values can you store in it?
__________________
edon1337 is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 02-16-2017 , 14:14   Re: [ TUT ]Dynamic Array
Reply With Quote #16

You can store entries as much as you like, until you server runs out of memory.
The 1 you are passing, is the size of each Dynamic Array entry you are storing in the Dynamic Array.

1 is one integer, 2 are two integers in and array form.
So, if you use 2 or higher, you need to use ArrayGetArray and ArrayPushArray, to and add and remove them.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 02-16-2017 at 14:23.
addons_zz is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-16-2017 , 14:16   Re: [ TUT ]Dynamic Array
Reply With Quote #17

Quote:
Originally Posted by edon1337 View Post
@hamlet , after 2-3 weeks, I finally understood the purpose of arrays. Thanks so much for the explanation.

Question :
If you set the Array size 1 (ArrayCreate) , how many values can you store in it?
Any amount of values. That number is only size for each array entry. If you'll be doing ArrayPushCell, then size 1 is what you would want and if you'll be doing ArrayPushArray or ArrayPushString then that size would be the maximum size of every array/string you push/set.

Last edited by klippy; 02-16-2017 at 14:17.
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 02-16-2017 , 15:30   Re: [ TUT ]Dynamic Array
Reply With Quote #18

As it was already stated by addons and Klippy, that's why we have dynamic arrays, to store unlimited ammout of data. The size just determine what data type the array will hold.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-17-2017 , 07:36   Re: [ TUT ]Dynamic Array
Reply With Quote #19

If it can hold unlimited amount of data, then why does 'size' option exist..? So if I create an Array with size 1, I can put 100 numbers in it? Btw could you make a tutorial about Tries ? I know it exists but it's old and outdated.
__________________

Last edited by edon1337; 02-17-2017 at 07:37.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 02-17-2017 , 08:18   Re: [ TUT ]Dynamic Array
Reply With Quote #20

Do you even read the answers?

Addons said:
Quote:
The 1 you are passing, is the size of each Dynamic Array entry you are storing in the Dynamic Array.
Klippy said:
Quote:
That number is only size for each array entry
I said:
Quote:
The size just determine what data type the array will hold.
Let's try one more time:
PHP Code:
new Array:Test ArrayCreate(1
The 1 tells that you want to save single-cell data(basically numbers).

PHP Code:
new Array:Test ArrayCrete(some_other_number_here), where some_other_number_here 
This means that you will store multicell data(basically arrays). They can be strings or array of numbers.

PHP Code:
new Array:Test ArrayCreate(32
In "Test" you can save as many strings as you want, but one string can't be bigger than 32.

View the array like a list:
PHP Code:
new Array:Test ArrayCreate(1)

Translates to:
/*
var0
var1
var2
var3
.
.
.
varn
*/

new Array:Test ArrayCreate(32)
/*
var0[32]
var1[32]
var2[32]
.
.
.
.
varn[32]
*/ 
Quote:
So if I create an Array with size 1, I can put 100 numbers in it?
If you create an array with cellsize = 1(not of size 1), then you are creating an array of numbers. You can store as much data as you want, but data must be a number, not an array.
There is no cap.

PHP Code:
new Array:MyArray ArrayCreate(1)
for(new 
i9999i++)
{
     
ArrayPushCell(MyArrayi)

We just saved 9999 values.
__________________

Last edited by HamletEagle; 02-17-2017 at 08:22.
HamletEagle is offline
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

Forum Jump


All times are GMT -4. The time now is 16:21.


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