How do I find values and compare them in 2 arrays?

0 favourites
From the Asset Store
Use this game pack to create your own game, modify the existing game or simply take a look and see how it was made.
  • Hello guys! I need a little help, if someone can help me! :)

    I use 2 arrays and i try to compare them.

    ARRAY 1 (4,2,1)

    ---------------

    0 = A,0

    1 = B,0

    3 = C,0

    4 = D,0

    ARRAY 2 (4,1,1)

    ---------------

    0 = A

    1 = C

    3 = B

    4 = C

    So what means?

    0 = Default

    1 = Value exist in the same position for both arrays

    2 = Value exist in different position

    3 = Value not exist at all

    So when check the example above will give us this result:

    ARRAY 1 (4,2,1)

    ---------------

    0 = A,1

    1 = B,2

    3 = C,2

    4 = D,3

    Some help please? Thank you!

  • Hey there, you can compare two arrays values by using "For each element" loop and "Compare Current Value"

  • Hi, thanks for your reply!

    I know this part, but i'm stuck as i'm not looking just to compare values.

    1 = Value exist in the same position for both arrays

    2 = Value exist in different position

    3 = Value not exist at all

    Basically part 2 it's the complex part i have stuck.

    By the way thanks for your time!

  • If you've done 1 then you should be able to do 2 because 2 would just be an Else on 1.

  • Ah I understand better, you're describing different figure cases, and indeed case 2 is a bit more complex, what you can do is, instead of using For Each Element, use a For loop and use its loopindex to look through the values of the first array, and for each of these values, run another For loop through the second array, and if any of the values it contains is equal to array value at first For loopindex, stop the loop and trigger an event;

    I guess an example would be easier to understand, I'll give it a shot once at home

  • Yes example will work better, to see the result.

    But in the meantime i will try it myself.

    Thanks again guys! :)

    Also please note again:

    Array 1 gives me this example:

    0 = A,1

    1 = B,2

    3 = C,2

    4 = D,3

    And if you noted, in array2 below i use in position 4 same letter as position 1. But as result, array1 give me: 4 = D,3 (3 = value not exist)

    0 = A

    1 = C

    3 = B

    4 = C

    * Of course letter "C" exist twice in array 2, but in array 1 we need it only 1. So array find first if the value = same position, otherwise find if exist to somewhere else or not exist.

    In theory make sense, but i try to find a best solution.

  • Another example:

    Array1

    0=> A,2

    1=> B,2

    2=> C,1

    3=> D,1

    Array2

    0=> B

    1=> A

    2=> C

    3=> D

    2 = Value exist in different position.

    I hope make sense! :)

  • You need to grab the X and the value from the first array, then you run an 'array 2 contains value', this gives you 3 if it returns false. If true, then you run the further check, find the X of the passed value in the second array, if it matches the passed X from array 1 then returns 1 else it's 2.

  • Ok, thanks i will give a try!

  • Hi, i'm still looking for a solution, any help? Thanks! :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I posted the solution already, what did you try ?

  • lionz

    Is this easy and fast for you to share a source file? Thanks!

  • Alright I had some free time dropbox.com/s/se6or865jefqh5c/arraychecks.c3p

  • lionz first of all thank you very much!!!

    I check the file, it works almost perfect!

    There is only 1 issue.

    Do you know what missing below to this example:

    Array1

    H

    E

    L

    L

    O

    Array2

    H

    L

    E

    E

    O

    So, with your file the result is this:

    H,1

    E,2

    L,2

    L,2

    O,1

    But the problem here that the result needed to be:

    H,1

    E,2

    L,2

    L,3

    0,1

  • So basically, 1) all depend/starting with the unique values and 2) The order.

    Again, thanks a lot for your help, i appreciate it!!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)