[HELP] Foreach simple loop.
#1

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!
Reply
#2

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

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

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)
Reply
#5

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++) 
Reply
#6

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;
}
Reply
#7

Thank you.
This topic may close now.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)