PDA

View Full Version : Get value at index with foreach


Aviicix
11/04/2017, 02:46 PM
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:

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

Aviicix
11/04/2017, 03:14 PM
Thank you for replying ******,
I'm using foreach standalone 0.4 and there's no such function, what version do I need to download ?

Aviicix
12/04/2017, 09:50 AM
Bump

OneDay
12/04/2017, 09:54 AM
Try with ysi

Aviicix
12/04/2017, 10:09 AM
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

raydx
12/04/2017, 10:31 AM
Yeah, there is no Iter_Index function, i've checked.

Aviicix
13/04/2017, 09:26 PM
I fixed it by making this function:


#define Iter_Index(%1,%2) Itter_Index(_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_AR RAY_SIZE(%1))

stock Itter_Index(array[], index, size)
{
new cont = 0,
last = size,
next = array[last];
while(cont < index)
{
last = next;
next = array[last];
cont++;
}
return next;
}

Use:
new value = Iter_Index(IteratorName, index);


new Iterator:test<5>;
Iter_Add(test, 4);
Iter_Add(test, 0);
Iter_Add(test, 2);

Iter_Index(test, 1); // return 2 as the array will be ordered in ascendent order

DRIFT_HUNTER
13/04/2017, 10:38 PM
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:

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 ?

Aviicix
14/04/2017, 07:20 AM
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.