SA-MP Forums Archive
Get value at index with foreach - 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: Get value at index with foreach (/showthread.php?tid=632257)



Get value at index with foreach - Aviicix - 11.04.2017

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])


Re: Get value at index with foreach - Aviicix - 11.04.2017

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


Re: Get value at index with foreach - Aviicix - 12.04.2017

Bump


Re: Get value at index with foreach - OneDay - 12.04.2017

Try with ysi


Re: Get value at index with foreach - Aviicix - 12.04.2017

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


Re: Get value at index with foreach - raydx - 12.04.2017

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


Re: Get value at index with foreach - Aviicix - 13.04.2017

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 



Re: Get value at index with foreach - DRIFT_HUNTER - 13.04.2017

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 ?


Re: Get value at index with foreach - Aviicix - 14.04.2017

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.