SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 09/10/2019, 03:44 PM   #1
MajesticPTR
Little Clucker
 
Join Date: Jan 2016
Posts: 15
Reputation: 0
Default Iter_Remove in foreach loop problem.

Hello, when in a loop trying to delete a given value from an iterator returns an error.

Code:
Code:
#include <a_samp>
#include <YSI\y_iterate>

main(){}

new Iterator:Test<280>;

public OnGameModeInit()
{	
	for(new i; i < 30; i++) Iter_Add(Test, i);
	
	foreach(new i: Test)
	{
		printf("%d", i);
		if(i == 25)
		{
			Iter_Remove(Test, i);
		}
	}
	print("a");
	return 1;
}
Console:
Code:
[16:44:44]  
[16:44:44]  ======================================= 
[16:44:44]  |                                     | 
[16:44:44]  |        YSI version 1.00.0001        | 
[16:44:44]  |        By Alex "******" Cole        | 
[16:44:44]  |                                     | 
[16:44:44]  ======================================= 
[16:44:44]  
[16:44:44] 0
[16:44:44] 1
[16:44:44] 2
[16:44:44] 3
[16:44:44] 4
[16:44:44] 5
[16:44:44] 6
[16:44:44] 7
[16:44:44] 8
[16:44:44] 9
[16:44:44] 10
[16:44:44] 11
[16:44:44] 12
[16:44:44] 13
[16:44:44] 14
[16:44:44] 15
[16:44:44] 16
[16:44:44] 17
[16:44:44] 18
[16:44:44] 19
[16:44:44] 20
[16:44:44] 21
[16:44:44] 22
[16:44:44] 23
[16:44:44] 24
[16:44:44] 25
[16:44:44] 281
[16:44:44] [debug] Run time error 4: "Array index out of bounds"
[16:44:44] [debug]  Accessing element at index 281 past array upper bound 280
[16:44:44] [debug] AMX backtrace:
[16:44:44] [debug] #0 00002188 in public Itter_OnGameModeInit () at anty2.pwn:12
[16:44:44] [debug] #1 native CallLocalFunction () [004743b0] from samp-server.exe
[16:44:44] [debug] #2 00001470 in public ScriptInit_OnGameModeInit () at ...\pawno\include\YSI\y_iterate.inc:791
[16:44:44] [debug] #3 0000016c in public OnGameModeInit () at ...\pawno\include\YSI\y_scriptinit.inc:171
[16:44:44] Number of vehicle models: 0
MajesticPTR is offline   Reply With Quote
Old 09/10/2019, 03:59 PM   #2
Boarden
Little Clucker
 
Boarden's Avatar
 
Join Date: Jun 2014
Location: (Formerly Great) Britain
Posts: 32
Reputation: 4
Default Re: Iter_Remove in foreach loop problem.

Have you tried using Iter_SafeRemove?

Code:
#include <a_samp>
#include <YSI_Data\y_iterate>

main(){}

new Iterator:Test<280>;

public OnFilterScriptInit()
{	
	for(new i; i < 30; i++) Iter_Add(Test, i);
	
	foreach(new i : Test)
	{
		if(i == 25)
		{
			Iter_SafeRemove(Test, i, i);
			continue;
		}

		printf("%d", i);
	}	
	return 1;
}
This seems to give the outcome you was going for. (I assume)
Boarden is offline   Reply With Quote
Old 09/10/2019, 06:13 PM   #3
raydx
Huge Clucker
 
raydx's Avatar
 
Join Date: Feb 2017
Posts: 205
Reputation: 73
Default Re: Iter_Remove in foreach loop problem.

You can't use iter_remove inside foreach.
raydx is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach Iter_Remove (client exceeded 'ackslimit') Vizi10 Scripting Help 2 31/07/2019 02:09 PM
[Ajuda] foreach Loop GasparzinhoXD Português/Portuguese 3 01/07/2019 11:11 PM
foreach and loop? ivndosos Scripting Help 4 07/03/2018 02:09 AM
help loop foreach kbalor Scripting Help 9 25/09/2013 07:38 PM
problem with loop and foreach Trenico Scripting Help 1 28/01/2012 11:46 PM


All times are GMT. The time now is 12:01 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.