# How do I update an array element based an another array?

Just started using Construct 2? Post your questions here

### » Sun Mar 19, 2017 10:01 am

I have these two arrays, namely ArrayForSequencying and ArrayForMatchGame.
I need to replace the the 2nd value in ArrayForMatchGame based on the 3rd value in Array for Sequencing
if the 1st value of ArrayForMatchGame <> 0 and is equal to the 2nd value in ArrayForSequencing:

ArrayForSequencing
0 with 2, 5, 1
1 with 3, 4, 1
2 with 4, 1, 2
3 with 5, 2, 2
4 with 6, 3, 3
and so on

ArrayForMatchGame
0 with 0,0 should be left as is
1 with 3,0 should become 3, 3
2 with 5,0 should become 5, 1
3 with 2,0 should become 2, 2
4 with 1,0 should become 1, 2
5 with 4,0 should become 4, 1
and so on

I have tried various combinations of "For each XY element" or "For each X element" "For" loops but I am still struggling to get it right. (I tried pasting the image to this post, but nothing comes out when I page between the img placeholders.) Thanks in advance.
B
11
S
2
G
1
Posts: 10
Reputation: 1,095

### » Sun Mar 19, 2017 12:24 pm

To save yourself from endless looping, just use a lookup table.

In c2, the most easy lookup table is a dictionary.
But, lets do it with the second best candidate: a 1D array.

Using a 1D array, we can use .....
The condition > Array > Contains value (value)
The expression > Array.IndexOf( value )
..... to get an the index of a found value in return

Both scan the whole array, and that is not what we need, so i made a 1D lookup table (event 22) with only the values that they are allowed to scan (the 2nd value in ArrayForSequencing)

From there it is easy, as you see :

https://www.dropbox.com/s/qpclzk430wxf3 ... .capx?dl=0
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sun Mar 19, 2017 1:10 pm

Thank you very much.
B
11
S
2
G
1
Posts: 10
Reputation: 1,095