Get value at index with foreach
#1

Hi guys,

I have a foreach iterator which stores some array indexes and I use it in a dialog, now I need to access the original array at the index stored in the "listitem" index of the foreach iterator, someone know how can I do this?

e.g:
pawn Код:
new IntArray[][E_INT_INFO] = {
{0.0000, 0.000, 3.000},
{124.1524, -67.2987, 1.5782}
// ...
};

Iter_Add(IntIter, 1); // Adding the second element of IntArray
Iter_Add(IntIter, 7);
Now, when I use the foreach in the dialog it shows IntArray[1] at index 0 intArray[7] at index 1 and so on..
I'd like to get the value from the foreach iterator at index "listitem" (Intiter[listitem])
Reply
#2

Thank you for replying ******,
I'm using foreach standalone 0.4 and there's no such function, what version do I need to download ?
Reply
#3

Bump
Reply
#4

Try with ysi
Reply
#5

I've checked the YSI github repository looking for y_iterate and foreach and I didn't find any Iter_Index function, I read something about this function on Gammix's foreach but I don't want to rewrite all the things I've already made
Reply
#6

Yeah, there is no Iter_Index function, i've checked.
Reply
#7

I fixed it by making this function:

PHP код:
#define Iter_Index(%1,%2) Itter_Index(_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
stock Itter_Index(array[], indexsize)
{
    new 
cont 0,
        
last size,
        
next = array[last];
    while(
cont index)
    {
        
last next;
        
next = array[last];
        
cont++;
    }
    return 
next;

Use:
PHP код:
new value Iter_Index(IteratorNameindex); 
PHP код:
new Iterator:test<5>;
Iter_Add(test4);
Iter_Add(test0);
Iter_Add(test2);
Iter_Index(test1); // return 2 as the array will be ordered in ascendent order 
Reply
#8

Quote:
Originally Posted by Aviicix
Посмотреть сообщение
Hi guys,

I have a foreach iterator which stores some array indexes and I use it in a dialog, now I need to access the original array at the index stored in the "listitem" index of the foreach iterator, someone know how can I do this?

e.g:
pawn Код:
new IntArray[][E_INT_INFO] = {
{0.0000, 0.000, 3.000},
{124.1524, -67.2987, 1.5782}
// ...
};

Iter_Add(IntIter, 1); // Adding the second element of IntArray
Iter_Add(IntIter, 7);
Now, when I use the foreach in the dialog it shows IntArray[1] at index 0 intArray[7] at index 1 and so on..
I'd like to get the value from the foreach iterator at index "listitem" (Intiter[listitem])
IntArray is constant array...why would you need to use foreach instead of while/for ?
Reply
#9

You're right but what I've posted right there is the testing code I was trying, I needed foreach because instead of making other class-specific array which will just be a waste of memory I decided to store the indexes of the elements of a specific sub-class on his specific iterator.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)