How do I compare entire lists?

0 favourites
  • 9 posts
  • I want to check if list 1 at least contains all of the items in list 2, regardless of the order. List 1 could have the same elements in the same order, the same elements in a different order, or the same elements with additional elements and it should return True.

    How would I do this?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The best way that comes to my mind is to sort both lists (Make copies if you need the unsorted data) and compare those using some loops.

  • Are there any functions to help organize alphabetically?

  • Zygorithm try to look at array. Array have condition that can check whether ArrayA.CurValue exist in ArrayB. And there's action to manipulate the array, to sort it. Arranging integer works for me (only ascending supported) but I haven't tried sorting strings.

  • Zygorithm ... I'm not sure why you would have to sort the data if you just want to compare lists. For example, if you want to see if all elements in ListB are in ListA, just do a loop against ListB and then find() against ListA using loopindex .... Something like this:

    add global variable "ListContainsAll" = "true"

    add system loop For "ListCompare" from 1 to tokencount(listB, "," ) [assumes your list delimiter is a comma]

    add sub event condition compare two values: find(ListA, tokenat( ListB, loopindex("ListComapre"), ",") ) = -1 --> set ListContainsAll ="false"

    If you run the above and all elements in ListB are in ListA, you should end up with "ListContainsAll" still true, otherwise false.

  • Here is a capx demo of the above.

    EDIT: If you need to be more precise and to compare against each element of ListA ( which may be necessary depending on your data), your sub event would be a second loop, comparing each element individually.

    For example if ListA = "onething,somthingelse" ... and ListB = "some,anotherthing" -- the capx would still return true (because find(ListA, "some") would still be true - "some" is in "somethingelse") ... so if there is potential 'data overlap' in your lists, you would need to use a second inner loop to explicitly compare each element.

    ... I hope this helps.

  • ...update: actually the 'more precise' method turns out to be more complicated than I thought, because again there is no 'tokenfind' type of feature -- you may very well have to load both lists into arrays. I'll let someone more experienced answer that, but Ashley this is an example of my request for fleshing out the token functions. Why not have tokenfind or tokencompare built in? (or tokenappend, tokendelete, tokensort, etc for that matter?... ) ... it would make working with lists so much easier.

    (For an example of a development tool that does it right with lists, check out ColdFusion's list functions -- it would also appear C#, C++, etc has plenty of built in list methods.)

  • kbtbc something to be aware of is that your method gets exponentially more expensive the longer the list is. Sorting then comparing gets expensive a lot more 'slowly'.

    The 'bruteforce' method of looping through 'B' for every entry in 'A' should work fine provided your list is guaranteed to be small.

  • Well, I ended up assigning list 2 elements to a temp array and looping the elements in list 1 to compare against temp. I created a local variable for the length of temp list. Then I created another local variable for how many matches I found. Then I looped through list 1 and added 1 every time there was a match in list 2. If the number of matches equals the length of the temp array, then it returns true. As for expensiveness, the temp array size and list 1 size will never exceed 5 or have any duplicate data so I think it could be done reasonably quickly.

    Thanks for everyone's help!

    Construct 2 does not have any way to isolate array functions to a Z subsection of a 3D array (at least not that I can see from the documentation), which would have made things a lot easier.

    Also, Construct 2 should add booleans. It's very strange having true and false as strings.

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