This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Solved Need help to delete all items from an array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Strick3n
Member
Join Date: Apr 2013
Old 11-14-2023 , 13:05   Need help to delete all items from an array
Reply With Quote #1

Code:
public selectSub( client, menu, item )
{
	switch( item )
	{
		case 0: 
			showBanMenu( client );
		case 1: 
			showRemoveMenu( client );
	}
	
	if( item == 2 )
	{
		new arrSize = ArraySize( g_aBannedList );
		
		if( arrSize )
		{
			for( new i = 0; i < arrSize; i++ )
			{
				ArrayDeleteItem( g_aBannedList, i );
			}
			
			SaveBannedToFile( g_aBannedList, g_szBannedList );
			
			menu_cancel( client );
			show_menu( client, 0, "^n", 1 );
			
			client_print( 0, print_chat, "[%s] All banned players has been removed from the banned file!", PLUGIN_NAME );
			
			showSelectMenu( client );
		}
		
		else
		{
			client_print( client, print_chat, "[%s] Banned file is empty!", PLUGIN_NAME );
			
			showSelectMenu( client );
			
			return 1;
		}
		
		return 1;
	}
	
	return 1;
}
i keep getting this message when i'm trying to delete all array items

Code:
L 11/14/2023 - 19:59:06: Invalid item specified in ArrayDeleteItem (6:6)
L 11/14/2023 - 19:59:06: [AMXX] Displaying debug trace (plugin "AdvancedBans.amxx")
L 11/14/2023 - 19:59:06: [AMXX] Run time error 10: native error (native "ArrayDeleteItem")
L 11/14/2023 - 19:59:06: [AMXX]    [0] AdvancedBans.sma::selectSub (line 133)
please help me
__________________

Last edited by Strick3n; 11-15-2023 at 06:43.
Strick3n is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 11-14-2023 , 13:27   Re: Need help to delete all items from an array
Reply With Quote #2

ArrayDeleteItem had some bug several years ago. I have no idea if the dev team corrected it. Try to search more about the error.
__________________
Jhob94 is offline
xDrugz
Senior Member
Join Date: Jul 2011
Location: return 4;
Old 11-14-2023 , 13:59   Re: Need help to delete all items from an array
Reply With Quote #3

Quote:
Originally Posted by Jhob94 View Post
ArrayDeleteItem had some bug several years ago. I have no idea if the dev team corrected it. Try to search more about the error.
so you suggest him to use trie instead?
__________________

Last edited by xDrugz; 11-14-2023 at 14:02.
xDrugz is offline
Strick3n
Member
Join Date: Apr 2013
Old 11-14-2023 , 14:22   Re: Need help to delete all items from an array
Reply With Quote #4

i managed to solve the problem by using ArrayClear
__________________
Strick3n is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 11-14-2023 , 14:31   Re: Need help to delete all items from an array
Reply With Quote #5

Quote:
Originally Posted by Strick3n View Post
i managed to solve the problem by using ArrayClear
That’s for the best but now i wonder if the arraydeleteitem is still bugged

Edit:
What amxx version are you using?
__________________

Last edited by Jhob94; 11-14-2023 at 14:40.
Jhob94 is offline
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-14-2023 , 22:18   Re: Need help to delete all items from an array
Reply With Quote #6

Try not to use forward traversal to delete array elements as much as possible. After element 1 is deleted, element 2 will become element 1, and array size 3 will become 2. At this point, deleting element 2 will trigger an "index out of bounds".
The solution is to use reverse traversal, or reduce the loop every time an element is deleted

PHP Code:
// Optimal solution
ArrayClear(arrayHandle);

// reverse traversal
for (new elementId ArraySize(arrayHandle); elementId--;)
{
    
ArrayDeleteItem(arrayHandleelementId);
}

// forward traversal
for (new numElements ArraySize(arrayHandle); numElementsnumElements--)
{
    
ArrayDeleteItem(arrayHandle0);
}

// forward traversal (Poor quality code)
for (new numElements ArraySize(arrayHandle), elementIdelementId numElementselementId++)
{
    
ArrayDeleteItem(arrayHandleelementId);
    
elementId--;
    
numElements--;

Leech_v2 is offline
Strick3n
Member
Join Date: Apr 2013
Old 11-15-2023 , 06:44   Re: Need help to delete all items from an array
Reply With Quote #7

Quote:
Originally Posted by Jhob94 View Post
That’s for the best but now i wonder if the arraydeleteitem is still bugged

Edit:
What amxx version are you using?
1.8.2 as far as i know
__________________
Strick3n 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 21:51.


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