Raised This Month: $ Target: $400
 0% 

Arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 09-20-2013 , 17:59   Arrays
Reply With Quote #1

When an item goes deleted the order is restored or it just goes down with 1position? For example if we delete item 2 from an array with 4elems, how it will look like.. 0,1,3? If yes is any solution to be reordered and changed in the normal way? Does anyone knows how array: works here?
Clauu is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 09-20-2013 , 20:27   Re: Arrays
Reply With Quote #2

Are we talking about static...
Code:
new array[4]
...or dynamic...
Code:
new Array:hArray = ArrayCreate()
...arrays?
__________________

Last edited by Black Rose; 09-20-2013 at 20:28.
Black Rose is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 09-20-2013 , 22:20   Re: Arrays
Reply With Quote #3

You can't delete values from a static array as such, but you can however recreate something similar to the ArrayDeleteItem() function, and "pull" all of the values down, if you were to assume something like a cell with its value being 0 would be empty.

When you delete an item from a dynamic array, you are actually deleting a cell, therefore all cells that come after the one you deleted will drop 1 position, and the total array size will drop by 1.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 09-21-2013 , 03:20   Re: Arrays
Reply With Quote #4

It;s about dynamic arrays, when i delete an item and then loop again with the new size i get invalid cellvector errors for the deleted position so i guess that the new array it isn;t remaped.
Clauu is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 09-21-2013 , 03:37   Re: Arrays
Reply With Quote #5

Show your code and explain what your trying to do.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 09-21-2013 , 03:59   Re: Arrays
Reply With Quote #6

PHP Code:
function check_array() {
for(new 
data,i=0,size=ArraySize(arr_data);i<size;i++)
if((
data=ArrayGetCell(arr_data,i))==2) {
ArrayDeleteItem(arr_data,i);
break;
} } 
Now if the arr_data size is 4 and the position 2 is deleted then at the next function call it will throw a 'Invalid cellvector handle provided' error for that deleted position because it no longer exists and the array isn't reordererd with new keys.

Last edited by Clauu; 09-21-2013 at 04:00.
Clauu is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 09-21-2013 , 04:37   Re: Arrays
Reply With Quote #7

Your example doesn't delete the position 2, it deletes whatever position contains the value 2.
If you're going to post an example instead of your actual code, make sure that the example can produce the error.

I'm gonna guess that your actual loop doesn't contain break. Because then you will get that error unless you account for the removed entries.

Code:
/**  * Deletes an item from the array.  All items beyond it get shifted down 1 space.  *  * @param which         The array that contains the item to delete.  * @param item          The item to delete.  */ native ArrayDeleteItem(Array:which, item);

Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 6", "", "");         new Array:hArray = ArrayCreate(1,1);         for ( new i = 1 ; i <= 5 ; i++ )         ArrayPushCell(hArray, i);         new size = ArraySize(hArray);         for ( new i = 0 ; i < size ; i++ )         server_print("PRE: %d: %d", i, ArrayGetCell(hArray, i));         new deleted_items;         for ( new i = 0 ; i < size ; i++ ) {         server_print("Reading cell %d with value of %d", i - deleted_items, ArrayGetCell(hArray, i - deleted_items));         if ( ArrayGetCell(hArray, i - deleted_items) == 3 )             ArrayDeleteItem(hArray, i - deleted_items++);     }         size = ArraySize(hArray);     for ( new i = 0 ; i < size ; i++ )         server_print("POST: %d: %d", i, ArrayGetCell(hArray, i)); }

Code:
PRE: 0: 1
PRE: 1: 2
PRE: 2: 3
PRE: 3: 4
PRE: 4: 5
Reading cell 0 with value of 1
Reading cell 1 with value of 2
Reading cell 2 with value of 3
Reading cell 2 with value of 4
Reading cell 3 with value of 5
POST: 0: 1
POST: 1: 2
POST: 2: 4
POST: 3: 5
__________________
Black Rose is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 09-21-2013 , 05:14   Re: Arrays
Reply With Quote #8

'==2' was just an value example and not position to delete, the code posted is similar to original that contains ents and i;m checking if those are valid before deletion. Hmm seems like the new array is remaped as it should but why it keeps giving me those errors
PHP Code:
public remove_ents() {

    new 
size ArraySize(arr_ents);
    
    if(
size<1)
        return;

    new 
entid,timer,classname[32];
    for (new 
0sizei++) {
        
        if (!
is_valid_ent((entid ArrayGetCell(arr_entsi)))) {
            
ArrayDeleteItem(arr_entsi);
            
ArrayDeleteItem(arr_ents_timei);
            break;
        }
            
        
timer ArrayGetCell(arr_ents_timei);
        if(
timer == 0) {
            
pev(entidpev_classnameclassnamecharsmax(classname));
            if(
equali(classname"GENT"))
                
engfunc(EngFunc_RemoveEntityentid);
            
ArrayDeleteItem(arr_entsi);
            
ArrayDeleteItem(arr_ents_timei);
        }
        else
            
ArraySetCell(arr_ents_time,itimer 1);
    }


Last edited by Clauu; 09-21-2013 at 05:16.
Clauu is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 09-21-2013 , 05:30   Re: Arrays
Reply With Quote #9

The problem is you delete one cell without recounting or subtract the number of removed indexes.
The code works great except for 2 things.
* If you delete one item, the one after will be ignored because it was shifted to the position of the first one.
* When the loop reaches the end, it doesn't know that the size has changed. This is the reason it throws an error, you're trying to retrieve values outside of the array range.

Look at the example I gave. Especially the variable deleted_items.
__________________

Last edited by Black Rose; 09-21-2013 at 05:30.
Black Rose is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 09-21-2013 , 06:24   Re: Arrays
Reply With Quote #10

Ups i forgot to add a break to the timer condition also, thanks
Clauu 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 19:03.


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