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.
  • So with the current method it gives this:

    A = a b b c

    B = b b d b

    C = 2 1 3 2

    For duplicates and such you need to give each Array element a unique number

    A = a1 b1 b2 c1

    B = b1 b2 d1 b3

    C = 2 2 3 3

    But I guess that doesn’t match what you were after. Probably would have to do it in three passes or something.

    1st pass just marks the 1’s

    A = a b b c

    B = b b d b

    C = _ 1 _ _

    2nd pass ignores the 1 matches and marks the 2 matches one by one.

    A = a _ b c

    B = b _ d b

    C = 2 1 _ _

    A = a _ _ c

    B = _ _ d b

    C = 2 1 _ _

    Last pass just marks the remainders as 3

    C = 2 1 3 3

    Implementing that in code is just another step. I don’t have a lot of time for that presently. I’m backlogged.

  • alextro Yes exactly. Any idea, how solve it?

    R0J0hound post this before:

    A:for each x
    — compare: A.at(A.curX)=B.at(A.curX)
    — — c: set at (a.curX) to 1
    — else
    — A: contains value B.at(A.curX)
    — — c: set at (a.curX) to 2
    — else
    — — c: set at (a.curX) to 3

    and works perfect so far, but alextro if you have different way, it's ok!

    Ok R0J0hound , thanks again for your time!

  • Still research with permutation thing if this kind of thing could be applicable in your case.

  • Thanks alextro for your help!!! :)

  • Hello alextro , R0J0hound ,

    is there a possibility to spend free time to check the part that missing?

    Many thanks guys, i appreciate it!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yes I am still struggling with storing how many times the letter has already taken in correct prioritize order. I insist to use array for that part but probably need to go back using Dictionary which is relatively easy to calling string. The sorting part done by listing available alphabet into a variable.

  • If you’re okay with the two arrays getting destroyed this should work on paper. You could always save the asjson of arrays A and B to save them. This assumes all the arrays have the same size.

     var i
    var f
    repeat a.width times
    -- set i to loopindex
    -- compare: a.at(i) = b.at(i)
    -- -- a: set at i to "-"
    -- -- b: set at i to "-"
    -- -- c: set at i to 1
    -- else
    -- -- set f to b.indexOf(a.at(i))
    -- -- compare f = -1
    -- -- -- c: set at i to 3
    -- -- else
    -- -- -- a: set at i to "-"
    -- -- -- b: set at f to "-"
    -- -- -- c: set at i to 2
  • Hi R0J0hound , first of all many thanks, very good example so far.

    But i found a strange issue, take a look below my result:

    Example1:

    #__---a---______---b---_______---c---

    0____H__________O___________2___

    1____E_____ _____E___________1___

    2____L___________L___________1___

    3____L___________L___________1___

    4____O__________H___________2___

    True!

    Example2:

    #__---a---______---b---_______---c---

    0____H__________O___________2___

    1____E_____ _____E___________1___

    2____L___________L___________1___

    3____L___________O___________3___

    4____O__________H___________2___

    I change line 3 and it's True again! All good... but:

    Example3:

    #__---a---_______---b---_______---c---

    0____H__________O___________2___

    1____E_____ _____E___________1___

    2____L___________O___________2___

    3____L___________L___________3___

    4____O__________H___________2___

    ... This time i change line 2, and here is the problem.

    For some reason line 3 gives me 3 instead 1. And line 2 gives me 2 instead 3.

    Also i give a different letter and get wrong result again:

    Example3:

    #__---a---_______---b---_______---c---

    0____H___________D___________2___

    I get 2 instead 3.

    Thank you!

  • Right. You have to loop over it in multiple passes. Once to find the 1s and another for the rest.

     var i
    var j
    repeat a.width times
    -- set i to loopindex
    -- compare: a.at(i) = b.at(i)
    -- -- a: set at i to "-"
    -- -- b: set at i to "-"
    -- -- c: set at i to 1
    
    repeat a.width times
    -- set i to loopindex
    -- compare: a.at(i) = "-"
    -- -- 
    -- else
    -- -- set f to b.indexOf(a.at(i))
    -- -- compare f = -1
    -- -- -- c: set at i to 3
    -- -- else
    -- -- -- a: set at i to "-"
    -- -- -- b: set at f to "-"
    -- -- -- c: set at i to 2
  • Hi R0J0hound unfortunately not working.

    Also in your part, is missing something?

    1. -- compare: a.at(i) = "-"
    2. -- --
    3. -- else

    You compare "a" without set a value? Just in case missing...

    Can you test it to your construct 2 or 3 and share a file to check?

    Thank you!

  • It was intentionally missing. Probably could be reworked without it.

    That’s fair. I can only approximate events with text, and I was only mentally working it out. I’ll see if I can make a capx of it eventually. Might have to defer to other people’s ideas in the meantime as I have no eta when I’ll lay aside time to make one.

  • Ok, thanks again for your help!!!

  • Came out like I described, unless I made a typo.

    dropbox.com/s/t2olbf79b3han1q/Array_compare.capx

  • R0J0hound First of all many thanks, awesome example!

    I found a bug. Just change second text box to this:

    o e o l d

    And you will get this result:

    3 1 3 1 2

    But "d" not exist at all. And for some reason as "o" exist to array 1 gives me 3 as result to both.

    I think something, maybe can help you to find what line need to change or remove. So, what about the first step to be a full calculation if not exist a letter at all, so to completely remove it and gives 3. And then continue with other steps... Do you think can works?

  • It’s working as expected. In mine it sets the values based how A matches B.

    You’re looking at how B matches A.

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