SA-MP Forums Archive
[HELP] Foreach simple loop. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: [HELP] Foreach simple loop. (/showthread.php?tid=458358)



[HELP] Foreach simple loop. - sKgaL - 16.08.2013

Hello,
I want to make this loop -
Код:
for(new i;i<90;i++)
But in foreach way.
So how do I do it ?

Thanks alot!


Re : [HELP] Foreach simple loop. - Garwan50 - 16.08.2013

pawn Код:
foreach (new i : 90)
i think its correct


Re: Re : [HELP] Foreach simple loop. - sKgaL - 16.08.2013

Quote:
Originally Posted by Garwan50
Посмотреть сообщение
pawn Код:
foreach (new i : 90)
i think its correct
No it isn't right,
I've tryed it before.


Re: [HELP] Foreach simple loop. - Richie© - 16.08.2013

You have to create the Iterator then add and remove values from it, then you can use foreach.
Like this:
pawn Код:
new Iterator:_Admins<MAX_PLAYERS>;
Iter_Add(_Admins, playerid);
Iter_Remove(_Admins, playerid );

foreach(_Admins, i)



Re: [HELP] Foreach simple loop. - sKgaL - 18.08.2013

Quote:
Originally Posted by Richie©
Посмотреть сообщение
You have to create the Iterator then add and remove values from it, then you can use foreach.
Like this:
pawn Код:
new Iterator:_Admins<MAX_PLAYERS>;
Iter_Add(_Admins, playerid);
Iter_Remove(_Admins, playerid );

foreach(_Admins, i)
Could you give me the simple exmple like i've mentained here ?
PHP код:
for(new i;i<90;i++) 



Re: [HELP] Foreach simple loop. - Pottus - 18.08.2013

It's very easy to use here is a basic example.

pawn Код:
#include <a_samp>
#include <zcmd>
#include <YSI\y_iterate>

// Initialize a new iterator
new Iterator:TestIter<90>;

// Add some random iterations
public OnFilterScriptInit()
{
    Iter_Add(TestIter, 2);
    Iter_Add(TestIter, 5);
    Iter_Add(TestIter, 10);
    Iter_Add(TestIter, 53);
    Iter_Add(TestIter, 63);
    return 1;
}

// Add a new iteration
CMD:additer(playerid, arg[])
{
    // Find the next free iteration
    new index = Iter_Free(TestIter);

    // Make sure there is a free slot
    if(index > -1)
    {
        new line[128];
        format(line, sizeof(line), "Iterator Added Index: %i", index);
        SendClientMessage(playerid, -1, line);

        // Add the new iterator
        Iter_Add(TestIter, index);
    }
    // Iterator has no free indexes
    else SendClientMessage(playerid, -1, "Iterator is full!");
    return 1;
}

// Remove a specific iteration
CMD:removeiter(playerid, arg[])
{
    // Get the input value
    new index = strval(arg);

    // Does this index actually exist in the iterator?
    if(Iter_Contains(TestIter, index))
    {
        new line[128];
        format(line, sizeof(line), "Iterator Removed Index: %i", index);
        SendClientMessage(playerid, -1, line);
   
        // Remove the iterator
        Iter_Remove(TestIter, index);
    }
    // Index did not exist
    else SendClientMessage(playerid, -1, "Iterator does not exist!");
    return 1;
}

// Show all iterations
CMD:showiter(playerid, arg[])
{
    // Loop through the test iterator and show values this should message the player with by defaul
    // Iterator found at index: 2
    // Iterator found at index: 5
    // Iterator found at index: 10
    // Iterator found at index: 53
    // Iterator found at index: 63

   
    foreach(new i : TestIter)
    {
        new line[128];
        format(line, sizeof(line), "Iterator found at index: %i", i);
        SendClientMessage(playerid, -1, line);
    }
    return 1;
}



Re: [HELP] Foreach simple loop. - sKgaL - 19.08.2013

Thank you.
This topic may close now.